使用带有联合类型的打字稿在React组件上进行Visual Studio Code智能感知

时间:2018-09-18 12:01:13

标签: reactjs typescript visual-studio-code

所以我有一个使用Create-React-App的Typescript项目,并且我使用Visual Studio Code作为编辑器。

Intellisense通常可以很好地工作,但是在这种情况下,它不能按预期工作。

如果我有一个界面:

interface A { name: string }
interface B extends A { type: 'B'; myProp: string }
interface C extends A {type: 'C'; secondProp: string}

如果我然后有一个反应道具,其道具是:

const Component: React.SFC<B | C> = props => {}

如果我然后在另一个组件中,在render方法中创建Component的实例,然后执行以下操作:

<Component type='B' />

如果我在组件中键入myPop并尝试分配一个对象,否则我将不会获得智能感知'myProp'的智能感知,否则会出现错误提示期望字符串。因此看来它确实可以识别道具,只是不会在智能感知的自动完成部分弹出。

有点烦人的是,它们在智能自动完成列表中不提供B的“ myProp”或C的“ secondProp”。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

它可能与TypeScript issue #26004有关。随时提出新的TypeScript问题并引用该问题。

相关问题