我试图将defaultProps与具有枚举属性的组件一起使用。组件的props接口在另一个文件中定义,在defaultProps中分配的枚举值也是如此。
这很好,但是当我编译项目并检查由TSC生成的声明文件用于组件时,它正确地生成了defaultProps的类型,但是枚举没有导入语句。
应用程序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;

类型.tsx
export enum AnimalEnum {
    Cat = "Cat",
    Dog = "Dog"
}

export interface IAppProps {
    animal: AnimalEnum;
}

默认值.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"
  ]
}

typescript版本是^3.0.3

最佳答案

看起来像只虫子。我reported it。作为一种解决方案,如果在defaultProps字段中添加类型注释(即使它只是{animal: typeof defaults.animal}),生成的声明文件似乎是有效的。

10-06 03:51