简体   繁体   English

是否可以在 VSCode(或其他地方)中显示打字稿类型/接口的完整计算类型

[英]Is it possible to display the full computed type of a typescript type/interface in VSCode (or elsewhere)

When you hover over a type in VSCode it shows the type, but if you have a larger type it usually displays it as { a: string; ... 24 more ...; z: string }当您将鼠标悬停在 VSCode 中的类型上时,它会显示该类型,但如果您有更大的类型,它通常会将其显示为{ a: string; ... 24 more ...; z: string } { a: string; ... 24 more ...; z: string } { a: string; ... 24 more ...; z: string } . { a: string; ... 24 more ...; z: string } Is it possible to get it to display the full type somehow?是否有可能让它以某种方式显示完整类型?

Similarly, if you have an intersection of two types then sometimes it will just display it as Type1 & Type2 instead of displaying the full type, or if you use the Pick then it will display Pick<Type1, "a" | ... 23 more ... | "z">同样,如果您有两种类型的交集,那么有时它只会将其显示为Type1 & Type2而不是显示完整类型,或者如果您使用Pick那么它会显示Pick<Type1, "a" | ... 23 more ... | "z"> Pick<Type1, "a" | ... 23 more ... | "z"> Pick<Type1, "a" | ... 23 more ... | "z"> ... Pick<Type1, "a" | ... 23 more ... | "z"> ...

In all of these cases I think it would be useful to check my understanding of what typescript is doing to be able to see the full type sometimes and was wondering if there was a way to do that, whether through vscode or somehow through typescript's tools.在所有这些情况下,我认为检查我对 typescript 正在做什么以有时能够看到完整类型的理解会很有用,并且想知道是否有办法做到这一点,无论是通过 vscode 还是通过 typescript 的工具。

No, there isn't.不,没有。 However the latest release of TS does make this better than it was before.然而,最新版本的 TS 确实比以前更好。 When I am troubleshooting types I use a few techniques to unravel complex types.当我对类型进行故障排除时,我使用一些技术来解开复杂的类型。

The biggest on is to create types for the properties you are trying to inspect.最重要的是为您要检查的属性创建类型。 Then you can index in the object to inspect the types.然后您可以在对象中建立索引以检查类型。 Not ideal, but easy, and fast.不理想,但简单,快速。

const object1 = { a1: 4, b1: "test", c1: true, d1: ["test", "test2"], e1: { ea1: "yes", eb1: 3, ec1: [4] } as const } as const;
const object2 = { a2: 4, b2: "test2", c2: false, d1: ["testw", "testw2"], e2: { ea2: "no", eb2: 5, ec2: [8] } as const } as const;
const object3 = { a3: 4, b3: "test", c3: true, d3: ["test", "test2"], e3: { ea3: "never", eb3: 3, ec3: [4] } as const } as const;
const object4 = { a4: 4, b4: "test2", c4: false, d4: ["testw", "testw2"], e4: { ea4: "maybe", eb4: 5, ec4: [8] } as const } as const;
type testComplexType = typeof object1 & typeof object2 & typeof object3 & typeof object4;

type whatTypeIsThis = testComplexType["e1"]["ea1"]; //yes
type whatTypeIsThis2 = testComplexType["e2"]["ea2"]; //no
type whatTypeIsThis3 = testComplexType["e3"]["ea3"]; //never
type whatTypeIsThis4 = testComplexType["e4"]["ea4"]; //maybe

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

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