Deep partials are SUPER useful and not natively supported by TypeScript. Here, I use one to help with mocking an entity in a (imaginary) test file.
type DeepPartial<T> = T extends Function
? T
: T extends Array<infer InferredArrayMember>
? DeepPartialArray<InferredArrayMember>
: T extends object
? DeepPartialObject<T>
: T | undefined;
interface DeepPartialArray<T> extends Array<DeepPartial<T>> {}
type DeepPartialObject<T> = {
[Key in keyof T]?: DeepPartial<T[Key]>
}
interface Post {
id: string;
comments: {value: string}[]
meta: {
name: string;
description: string
},
call: Function
}
const post:DeepPartial<Post> = {
id: "123",
comments: [
{value: "123"}
]
}
标签:Typescript,string,DeepPartial,comments,extends,Tips,id From: https://www.cnblogs.com/Answer1215/p/16775530.html