【问题标题】:typescript: namespace import + specific import打字稿:命名空间导入+特定导入
【发布时间】:2018-02-24 20:12:10
【问题描述】:

我试图解决的问题

import * as React from 'react'; import {Component} from 'react';

我不喜欢它是两行 - 坦率地说,我的 linter 设置甚至不允许它。

但是写下面 import * as React from 'react'; const Component = React.Component; 对我来说也不太好——因为我将有 llines 的导入而不是 const delarations——它们实际上是导入

在非 TS 语法中,上述行可能是

import React, {Component} from 'react';

这就是我要找的,但是

import * as React, {Component} from 'react'; 不是有效的语法。

问题

我怎样才能实现一个具有命名空间导入和部分导入的单行代码

【问题讨论】:

  • 你试过import * as React from 'react'然后class A extends React.Component {}
  • @Aaqib 很好 - 这会起作用 - 但我的意思是更多的一般例子

标签: javascript reactjs typescript


【解决方案1】:

从 TypeScript 2.7 开始,您可以在 tsconfig.json 中启用 esModuleInterop 以启用 import React, { component } from 'react'

import * as React from 'react'有一个特定的含义,就是获取模块react的模块命名空间对象。 由于它获取了模块中的所有内容,因此使用React.component 然后使用import { component } from 'react' 提取component 更自然。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 2016-11-20
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多