Giter Site home page Giter Site logo

Comments (8)

Hotell avatar Hotell commented on May 21, 2024 3

@garth what you should do in case of importing react stuff is following:

tsonfig:

{
  "compileOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

now you can:

import React, { StatelessComponent } from 'react';

PRO TIP: instead of importing super long/verbose StatelessComponent use SFC type alias

PRO TIP 2: We've gone even further and import only what the module needs. So for transforming JSX to function calls we import only createElement

// now you got both runtime function and typescript type in one import
import {createElement, SFC} from 'react'

tsonfig:

{
  "compileOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "jsx": "react",
    "jsxFactory": "createElement",
  }
}

or since TS 2.9 you can import types inline where your need them, so you solely import only runtime values via standard ES2015 modules

import React from 'react'

type Props = {...}
const Hello: import('react').SFC<Props> = (props) => <div>Hello</div>

from tslint-config-standard.

garth avatar garth commented on May 21, 2024 2

I don't think that is valid syntax.

from tslint-config-standard.

blakeembrey avatar blakeembrey commented on May 21, 2024 1

You're right, sorry. I'm not sure then. I'd just use destructing like normal:

import * as React from 'react'
const { StatelessComponent } = React

from tslint-config-standard.

blakeembrey avatar blakeembrey commented on May 21, 2024 1

Ah, sorry, I just saw you were talking about type imports in the title 😄 I regularly overlook the title and just read the body, sorry for the confusion and oversight.

from tslint-config-standard.

blakeembrey avatar blakeembrey commented on May 21, 2024

Should be. Try import * as React, { StatelessComponent } from React.

from tslint-config-standard.

blakeembrey avatar blakeembrey commented on May 21, 2024

If you'd like, feel free to verify the behaviour is the same as with StandardJS. They're using it from https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-duplicates.md, so there may be a deviation in TSLint if your code was valid with ESLint.

from tslint-config-standard.

garth avatar garth commented on May 21, 2024

I think the rule is valid for js, just not sure it should be applied to ts. I have disabled the rule in local config, but I would be interested to see if there is a better solution.

from tslint-config-standard.

blakeembrey avatar blakeembrey commented on May 21, 2024

Both imports are identical in JS vs TS, there's no real difference. If what you mean is about StatelessComponent as a type, you can import from a namespace but it sadly gets ugly. Try this:

namespace React {
    export interface StatelessComponent { }
}

import StatelessComponent = React.StatelessComponent

This would be improved 10x if TypeScript supported destructing in this case, which i think is pretty important for property parity with JS nowadays (e.g. import { Component } = require('react') should be allowed by TypeScript too, but the only allow import React = require('react')).

from tslint-config-standard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.