[英]Typescript not emitting enum import when using defaultProps
I am trying to use defaultProps
with a component that has Enum prop. 我试图使用defaultProps
与组件具有Enum prop。 The props interface for the component is defined in another file and so is the enum value assigned in defaultProps
. 组件的props接口在另一个文件中定义, defaultProps
分配的枚举值也是如此。
This works fine but when I compile project and check the declaration file generated by tsc for the component, it generates the type for defaultProps
correctly but the enum doesn't have the import statement. 这工作正常,但是当我编译项目并检查tsc为组件生成的声明文件时,它会正确生成defaultProps
的类型,但枚举没有import语句。
App.ts App.ts
import * as React from 'react';
import './App.css';
import { defaults } from './defaults';
import { IAppProps } from './types';
import logo from './logo.svg';
class App extends React.Component<IAppProps> {
public static defaultProps = {
animal: defaults.animal
}
public render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.tsx</code> and save to reload.
</p>
</div>
);
}
}
export default App;
types.tsx types.tsx
export enum AnimalEnum {
Cat = "Cat",
Dog = "Dog"
}
export interface IAppProps {
animal: AnimalEnum;
}
defaults.ts defaults.ts
import { AnimalEnum } from "./types";
export const defaults = {
animal: AnimalEnum.Cat
}
Emitted file App.d.ts 发出的文件App.d.ts
import * as React from 'react';
import './App.css';
import { IAppProps } from './types';
declare class App extends React.Component<IAppProps> {
static defaultProps: {
animal: AnimalEnum; //<---------AnimalEnum doesn't have import!
};
render(): JSX.Element;
}
export default App;
tsconfig.json tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "build/dist",
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": false,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "src",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"declaration": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true
},
"exclude": [
"node_modules",
"build",
"scripts",
"acceptance-tests",
"webpack",
"jest",
"src/setupTests.ts"
]
}
typescript version is ^3.0.3 打字稿版本是^ 3.0.3
Looks like a bug. 看起来像个bug。 I reported it . 我报了 。 As a workaround, if you add a type annotation on the defaultProps
field (even if it's just {animal: typeof defaults.animal}
), the generated declaration file seems to be valid. 作为解决方法,如果在defaultProps
字段上添加类型注释(即使它只是{animal: typeof defaults.animal}
),生成的声明文件似乎是有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.