有没有办法打印已解析的TypeScript类型?
我正在阅读,我在Typescript文档中看到了很多示例,其中列出了已解决的问题?类型的版本及其旁边的注释:有没有办法打印已解析的TypeScript类型?,typescript,types,Typescript,Types,我正在阅读,我在Typescript文档中看到了很多示例,其中列出了已解决的问题?类型的版本及其旁边的注释: type TypeName<T> = T extends string ? "string" : T extends number ? "number" : T extends boolean ? "boolean" : T extends undefined ? "undefined" : T extends Function ? "
type TypeName<T> =
T extends string ? "string" :
T extends number ? "number" :
T extends boolean ? "boolean" :
T extends undefined ? "undefined" :
T extends Function ? "function" :
"object";
type T0 = TypeName<string>; // "string"
type T1 = TypeName<"a">; // "string"
type T2 = TypeName<true>; // "boolean"
type T3 = TypeName<() => void>; // "function"
type T4 = TypeName<string[]>; // "object"
当类型出现混淆时,这当然可以帮助我,所以我想知道是否有任何方法可以让编译器提取相同的类型信息 如果将鼠标悬停在条件类型上,Visual Studio代码已经提供了此功能。对于您的示例,它看起来是这样的:
您是说“其他”,然后将鼠标悬停在vs代码中某个类型上以查看已解析的类型?你想要某种命令行实用程序使用编译器API来实现这一点吗?@TitianCernicova Dragomir我从未使用过VS代码,它是否已经提供了此功能?是的,如果有帮助,我可以提供一个屏幕截图来回答:)@TitianCernicova Dragomir啊,很好,我不知道。是的,那太好了:)
type FunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T];
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
interface Part {
id: number;
name: string;
subparts: Part[];
updatePart(newName: string): void;
}
type T40 = FunctionPropertyNames<Part>; // "updatePart"
type T41 = NonFunctionPropertyNames<Part>; // "id" | "name" | "subparts"
type T42 = FunctionProperties<Part>; // { updatePart(newName: string): void }
type T43 = NonFunctionProperties<Part>; // { id: number, name: string, subparts: Part[] }