检查接口是否有必填字段
Check if an interface has a required field
是否可以使用 Typescript 的条件类型检查接口是否具有必填字段?
type AllRequired = { a: string; b: string }
type PartiallyRequired = { a: string; b?: string }
type Optional = { a?: string; b?: string }
// Is it possible to change this, so the below works
type HasRequiredField<T> = T extends {} ? true : false
type A = HasRequiredField<AllRequired> // true
type B = HasRequiredField<PartiallyRequired> // true
type C = HasRequiredField<Optional> // false
是的,. It gets a bit ,但是您的类型没有它们,所以我不会担心它们。
以下是如何仅提取对象类型的非可选属性的键
type RequiredKeys<T> = { [K in keyof T]-?:
({} extends { [P in K]: T[K] } ? never : K)
}[keyof T]
然后你可以检查它是否有任何一个(如果 RequiredKeys<T>
是 never
那么它没有):
type HasRequiredField<T> = RequiredKeys<T> extends never ? false : true
这会给出您想要的结果:
type A = HasRequiredField<AllRequired> // true
type B = HasRequiredField<PartiallyRequired> // true
type C = HasRequiredField<Optional> // false
希望对您有所帮助;祝你好运!
找到了至少在 TS 3.8.3 中有效的另一种更简单的方法。
// if Partial<T> extends T none of the fields are required.
type HasRequiredFeilds<T> = Partial<T> extends T ? false : true
示例用例
type Info<TInfo> = Partial<TInfo> extends TInfo
? { info?: TInfo }
: { info: TInfo };
type A = { type: 'A' } & Info<{ abc?: string }>
type B = { type: 'B' } & Info<{ abc: string }>
// Valid
let a1: A = { type: 'A' };
// Valid
let a2: A = { type: 'A', info: {} };
// Valid
let a3: A = { type: 'A', info: { abc: 'hello' } };
// Invalid
let a4: A = { type: 'A', info: { abc: 3 } };
// Invalid
let b1: B = { type: 'B' }
// Invalid
let b2: B = { type: 'B', info: {} };
// Valid
let b3: B = { type: 'B', info: { abc: 'Hello' } };
// Invalid
let b4: B = { type: 'B', info: { abc: 5 } };
是否可以使用 Typescript 的条件类型检查接口是否具有必填字段?
type AllRequired = { a: string; b: string }
type PartiallyRequired = { a: string; b?: string }
type Optional = { a?: string; b?: string }
// Is it possible to change this, so the below works
type HasRequiredField<T> = T extends {} ? true : false
type A = HasRequiredField<AllRequired> // true
type B = HasRequiredField<PartiallyRequired> // true
type C = HasRequiredField<Optional> // false
是的,
以下是如何仅提取对象类型的非可选属性的键
type RequiredKeys<T> = { [K in keyof T]-?:
({} extends { [P in K]: T[K] } ? never : K)
}[keyof T]
然后你可以检查它是否有任何一个(如果 RequiredKeys<T>
是 never
那么它没有):
type HasRequiredField<T> = RequiredKeys<T> extends never ? false : true
这会给出您想要的结果:
type A = HasRequiredField<AllRequired> // true
type B = HasRequiredField<PartiallyRequired> // true
type C = HasRequiredField<Optional> // false
希望对您有所帮助;祝你好运!
找到了至少在 TS 3.8.3 中有效的另一种更简单的方法。
// if Partial<T> extends T none of the fields are required.
type HasRequiredFeilds<T> = Partial<T> extends T ? false : true
示例用例
type Info<TInfo> = Partial<TInfo> extends TInfo
? { info?: TInfo }
: { info: TInfo };
type A = { type: 'A' } & Info<{ abc?: string }>
type B = { type: 'B' } & Info<{ abc: string }>
// Valid
let a1: A = { type: 'A' };
// Valid
let a2: A = { type: 'A', info: {} };
// Valid
let a3: A = { type: 'A', info: { abc: 'hello' } };
// Invalid
let a4: A = { type: 'A', info: { abc: 3 } };
// Invalid
let b1: B = { type: 'B' }
// Invalid
let b2: B = { type: 'B', info: {} };
// Valid
let b3: B = { type: 'B', info: { abc: 'Hello' } };
// Invalid
let b4: B = { type: 'B', info: { abc: 5 } };