【发布时间】:2019-12-09 11:00:57
【问题描述】:
我想提取类中具有默认值的属性的键名。这些信息是否可用于类型系统?
class Foo {
hasDefault: boolean = true
alsoHasDefault: number = 42
noDefault: boolean
}
// How can this be implemented?
type DefaultPropertyNames<T> = ...
// Example output:
type FooDefaults = DefaultPropertyNames<Foo> // -> 'hasDefault' | 'alsoHasDefault'
【问题讨论】:
-
不,信息不在类型系统中。您可以创建另一种方法来指定默认值,但直接它不可用
标签: typescript typescript-types