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

JSo*_*oet 13 typescript visual-studio-code

当您将鼠标悬停在 VSCode 中的类型上时,它会显示该类型,但如果您有更大的类型,它通常会将其显示为{ a: string; ... 24 more ...; z: string }. 是否有可能让它以某种方式显示完整类型?

类似地,如果您有两种类型的交集,那么有时它只会将其显示为Type1 & Type2而不是显示完整类型,或者如果您使用Pick则它会显示Pick<Type1, "a" | ... 23 more ... | "z">...

在所有这些情况下,我认为检查我对 typescript 正在做什么以便有时能够看到完整类型的理解会很有用,并且想知道是否有办法做到这一点,无论是通过 vscode 还是通过 typescript 的工具。

Tim*_*Tim 2

不,没有。然而,最新版本的 TS 确实比以前更好了。当我对类型进行故障排除时,我使用一些技术来解决复杂的类型。

最重要的是为您要检查的属性创建类型。然后您可以在对象中建立索引来检查类型。并不理想,但简单、快速。

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
Run Code Online (Sandbox Code Playgroud)