Comments (8)
@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.
I don't think that is valid syntax.
from tslint-config-standard.
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.
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.
Should be. Try import * as React, { StatelessComponent } from React
.
from tslint-config-standard.
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.
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.
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)
- How to ignore d.ts files? HOT 1
- An in-range update of tslint is breaking the build 🚨 HOT 1
- An in-range update of @types/node is breaking the build 🚨 HOT 3
- Should installation instructions specifiy --save-dev? HOT 1
- WARNING when running with tslint-config-standard HOT 2
- An in-range update of tslint is breaking the build 🚨 HOT 1
- An in-range update of @types/node is breaking the build 🚨 HOT 1
- Linting for js files HOT 2
- Consider adding tslint-sonarts rules HOT 1
- Code that errors indentation when upgrading from v4 to v7 HOT 1
- no-multi-spaces false report on multiple spaces inside template literal HOT 3
- Using tslint-config-standard produces several warnings HOT 3
- Remove no-unused-variable (deprecated in tslint) HOT 8
- no-unused-variable is deprecated HOT 11
- Match standardjs for object-curly HOT 3
- suggestion: remove no-use-before-declare HOT 3
- Promises must be handled appropriately HOT 3
- Warnings when linting HOT 1
- How to migrate to the new eslint/typescript ? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tslint-config-standard.