【发布时间】:2019-02-21 10:29:47
【问题描述】:
当我们想查看变量的值时,我们只需要console.log(someVar)
如果我们想看看 type 背后是什么?
例子:
type SomeUnion = 'foo' | 'bar' | 'baz'
console.log(SomeUnion)
// of course above will not work, but what will do?
// is there any TS tool that I am missing?
附:对于那些可能不明白为什么需要这个的人,这里有一个更复杂的类型(来自TS docs):
type FunctionPropertyNames<T> = { [K in keyof T]:
T[K] extends Function ? K : never }[keyof T];
interface Part {
id: number;
name: string;
subparts: Part[];
updatePart(newName: string): void;
}
type T40 = FunctionPropertyNames<Part>; // "updatePart"
最后一行注释了类型的样子。目前我正在做什么来确定它是否真的看起来像这样:
let foo: T40 = 'bar' // gives error: type "bar" is not assignable to type "updatePart"
换句话说,知道隐藏在类型标识符后面的唯一方法是生成类型错误,这不应该是唯一的方法。
【问题讨论】:
-
我认为没有任何工具可以静态地执行此操作,但您可以尝试创建对象的实例并在其上调用 Object.keys()。看到这个stackoverflow.com/a/43910186/1486848
标签: typescript