【发布时间】:2022-11-23 13:04:41
【问题描述】:
我有一个带有 props 的类型,它可以是可选的,具体取决于泛型类型:
type MyType<R extends Record<string, string> | undefined, A extends string[] | undefined> = {
record: R
array: A
}
我有一个接受MyType对象的函数
const myFunction = <R extends Record<string, string> | undefined, A extends string[] | undefined>(myObject: MyType<R, A>)=>{
// ... //
}
例如,如果 R 未定义,我希望能够调用 myFunction 并在道具中省略 record
const record = getTheRecord() // Assuming getTheRecord() returns a undefined here
const array = ['a']
myFunction({
array
})
如何根据通用类型使一些道具可选?
【问题讨论】:
标签: typescript typescript-generics