简体   繁体   English

在 TypeScript 中调用一个以 union 作为返回类型的函数后,如何获得 vscode 自动完成功能?

[英]How do I get vscode autocomplete after calling a function with a union as its return type in TypeScript?

How do I get proper editor autocomplete after calling a function that returns a union type?调用返回联合类型的函数后,如何获得正确的编辑器自动完成功能?

For example, in the code below, after calling getProperty<ColorfulOrCircle>() (line 14), the variable b should give show me what properties I can access, but that doesn't happen.例如,在下面的代码中,在调用getProperty<ColorfulOrCircle>() (第 14 行)之后,变量b应该告诉我我可以访问哪些属性,但这不会发生。

1. interface Colorful {
2.   color: string;
3. }
4. interface Circle {
5.   radius: number;
6. }

8. type ColorfulOrCircle = Colorful | Circle;

10. function getProperty<T>(cc: T): T {
11.   return cc;
12. }

14. let b = getProperty<ColorfulOrCircle>({color: "red"})

15. b. ; // NO AUTOCOMPLETE

When I hover over b.color , I get this error message:当我将鼠标悬停在b.color上时,我收到以下错误消息: 在此处输入图像描述

And when I press CTRL + BACKSPACE after b.当我在b. , I receive only 'random' words. ,我只收到“随机”的话。

在此处输入图像描述

How should I type my function in order to have better intellisense?我应该如何键入我的函数以获得更好的智能感知?

When not specifying the generic type but letting typescript infer it, this works perfectly.当不指定泛型类型但让打字稿推断它时,这非常有效。 But as you can see, this function now requires a generic type in its declaration.但正如您所见,这个函数现在需要在其声明中使用泛型类型。 If you want to have a function that can take a generic parameter like the original function in your question, check out the playground link where I included one at the bottom.如果您想拥有一个可以像问题中的原始函数一样采用通用参数的函数,请查看我在底部包含的操场链接。

Code:代码:

interface Colorful {
    color: string;
}
interface Circle {
    radius: number;
}

type ColorfulOrCircle = Colorful | Circle;

function getProperty<T extends ColorfulOrCircle>(cc: T) {
    return cc;
}

let b = getProperty({ color: "red" })

b. ; // AUTOCOMPLETE

Playground 操场

暂无
暂无

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

相关问题 我如何让 typescript 根据 function2E977777777777777777777777777777777777777777777777777777777777777777777777777777777777BED18 的返回类型 functionC17A94F14Z 的返回类型来推断 function 的返回类型 - How do I get typescript to infer the return type of a function based on the return type on a function of its arguments 在TypeScript中,如何根据其参数之一的返回类型来确定函数的返回类型? - In TypeScript, how do I make a function's return type based on the return type of one of its arguments? 如何从 function 在 TypeScript 中创建一个以 union 作为其键类型的记录? - How can I make a Record with union as its key type in TypeScript, from a function? 如何将Typescript枚举中的整数转换为键的并集类型的键值? - How do I convert an integer from a Typescript enum to its key value as a type of the union of the keys? TypeScript-联合类型不起作用的函数返回 - TypeScript - Union type not working function return 如何解压返回类型为 Typescript 并可能是数组? - How can I unpack a return type that is a Typescript union and may be an array? 在Typescript中,我如何声明一个返回字符串类型数组的函数? - In Typescript how do I declare a function that return string type array? 在TypeScript中,如何键入函数的参数而不是返回值? - In TypeScript, how do i type a function's arguments but not the return value? 如果在 Typescript 中满足条件,如何断言联合类型的类型? - How do I assert the type of a union type if a condition is met in Typescript? 如何在TypeScript中获取类方法的返回类型 - How do I get the return type of a class method in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM