简体   繁体   English

使用带有联合类型的打字稿在React组件上运行Visual Studio Code intellisense

[英]Visual Studio Code intellisense on React component using typescript with union types

So i have a project in Typescript using Create-React-App and i am using Visual Studio Code as my editor. 所以我有一个使用Create-React-App的Typescript项目,并且我使用Visual Studio Code作为编辑器。

Intellisense generally works perfectly fine, however in this one scenario it doesn't work as expected. Intellisense通常可以很好地工作,但是在这种情况下,它不能按预期工作。

If i have an interface: 如果我有一个界面:

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

if i then have a react component whose props are: 如果我然后有一个反应成分,其道具是:

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

If i then in another component create an instance of Component in the render method and do something like: 如果我然后在另一个组件中,在render方法中创建Component的实例,然后执行以下操作:

<Component type='B' />

i don't get intellisense for the property 'myProp', if i type myPop in the component and try to assign an object or something it will error saying expecting a string. 如果我在组件中键入myPop并尝试分配一个对象或某些东西,它将提示您期望一个字符串,但我不会获得对“ myProp”属性的智能感知。 So it does seem that it recognises the props, just wont popup on the autocomplete part of intellisense. 因此,它似乎确实可以识别道具,只是不会在智能感知的自动完成部分弹出。

Just a bit annoying that it doesn't offer 'myProp' in case of B or 'secondProp' in case of C in the list of intellisens autocompletes. 有点烦人的是,在智能自动完成列表中,它在B的情况下不提供“ myProp”,在C的情况下不提供“ secondProp”。

Anyone offer any thoughts? 有人提供任何想法吗?

It's probably related to TypeScript issue #26004 . 这可能与TypeScript问题#26004有关 Feel free to file a new TypeScript issue and cite that one. 随时提出新的TypeScript问题并引用该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM