功能如下:
const getProp = <
TObj extends object,
TKey extends keyof TObj
>(props: TObj, key: TKey, def: TObj[TKey]): TObj[TKey] => {
if (props[key] == null) return def
return props[key]
}
如下使用时:
const fluid = getProp(props, 'fluid', true) // return type is boolean | undefined
返回类型boolean | undefined
虽然我期望并且需要它是boolean
.