我正在尝试将defaultProps
与具有Enum属性的组件一起使用。组件的props接口是在另一个文件中定义的,defaultProps
中分配的枚举值也是这样。
这正常工作,但是当我编译项目并检查由tsc为组件生成的声明文件时,它会正确生成defaultProps
的类型,但枚举没有import语句。
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
export enum AnimalEnum {
Cat = "Cat",
Dog = "Dog"
}
export interface IAppProps {
animal: AnimalEnum;
}
defaults.ts
import { AnimalEnum } from "./types";
export const defaults = {
animal: AnimalEnum.Cat
}
发射的文件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
{
"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"
]
}
打字稿版本为^ 3.0.3
答案 0 :(得分:0)
看起来像个错误。我reported it。解决方法是,如果在defaultProps
字段上添加类型注释(即使只是{animal: typeof defaults.animal}
),则生成的声明文件似乎是有效的。