[英]TypeScript reuse type generics
How to extend generic type generics of type to function?如何将类型的泛型类型 generics 扩展为 function? Is it possible to reuse generics without copy paste? generics 不复制粘贴可以重复使用吗?
type T<FormType extends 'edit' | 'create' = 'create', RecordType extends Record = Record> = {
type: FormType,
record: RecordType
}
const func = <FormType extends 'edit' | 'create' = 'create', RecordType extends Record = Record>(props: T<FormType, RecordType>) => ...
How to extend this <FormType extends 'edit' | 'create' = 'create', RecordType extends Record = Record>
如何扩展此<FormType extends 'edit' | 'create' = 'create', RecordType extends Record = Record>
<FormType extends 'edit' | 'create' = 'create', RecordType extends Record = Record>
from type generics to function generics without copy paste? <FormType extends 'edit' | 'create' = 'create', RecordType extends Record = Record>
from type generics to function generics没有复制粘贴?
Something you could do is break it down to one Base type:您可以做的是将其分解为一种基本类型:
type GenericType = {
formType: 'edit' | 'create'
recordType: Record
}
type Base = {
formType: 'edit';
recordType: Record
}
type T<B extends GenericType = Base> = {
type: FormType,
record: RecordType
}
const func = <B extends GenericType = Base>(props: T<B['formType'], B['record']>) => ...
I find this helps with verbosity a lot especially as I add a lot of generics in.我发现这对冗长有很大帮助,尤其是当我在其中添加了很多 generics 时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.