admin管理员组文章数量:1122832
I need to generate correct types for genStruct function, I understand how to make the function work but struggling to create correct types.
Here is my function
type Breakpoint = "sm" | "md" | "lg"
export const genStruct = <T extends object>(
defaultValue: T,
breakpoints: Breakpoint[],
): Partial<Responsive<T>> => { // Partial to allow omission of some keys
return breakpoints.reduce((acc, breakpoint) => {
acc[breakpoint] = Object.keys(defaultValue).length === 0
? ('' as any)
: { ...defaultValue }
return acc
}, {} as Partial<Responsive<T>>)
}
type NestedProps = {
rows: string
cols: string
}
type Responsive<T> = {
[key in Breakpoint]: T
}
type Default = Responsive<NestedProps>
const some_value: Default = {
sm: {
rows: '',
cols: '',
},
...genStruct({
rows: '',
cols: '',
}, ['md', 'lg'])
}
This is error generated by typescript
Type '{ sm: { rows: string; cols: string; }; md?: { rows: string; cols: string; } | undefined; lg?: { rows: string; cols: string; } | undefined; }' is not assignable to type 'Default'. Types of property 'md' are incompatible. Type '{ rows: string; cols: string; } | undefined' is not assignable to type 'NestedProps'. Type 'undefined' is not assignable to type 'NestedProps'.
I need to generate correct types for genStruct function, I understand how to make the function work but struggling to create correct types.
Here is my function
type Breakpoint = "sm" | "md" | "lg"
export const genStruct = <T extends object>(
defaultValue: T,
breakpoints: Breakpoint[],
): Partial<Responsive<T>> => { // Partial to allow omission of some keys
return breakpoints.reduce((acc, breakpoint) => {
acc[breakpoint] = Object.keys(defaultValue).length === 0
? ('' as any)
: { ...defaultValue }
return acc
}, {} as Partial<Responsive<T>>)
}
type NestedProps = {
rows: string
cols: string
}
type Responsive<T> = {
[key in Breakpoint]: T
}
type Default = Responsive<NestedProps>
const some_value: Default = {
sm: {
rows: '',
cols: '',
},
...genStruct({
rows: '',
cols: '',
}, ['md', 'lg'])
}
This is error generated by typescript
Type '{ sm: { rows: string; cols: string; }; md?: { rows: string; cols: string; } | undefined; lg?: { rows: string; cols: string; } | undefined; }' is not assignable to type 'Default'. Types of property 'md' are incompatible. Type '{ rows: string; cols: string; } | undefined' is not assignable to type 'NestedProps'. Type 'undefined' is not assignable to type 'NestedProps'.
1 Answer
Reset to default 0Ok I have the solution
export const genStruct = <T extends object, K extends Breakpoint>(
defaultValue: T,
breakpoints: K[],
): { [P in K]: T } => {
return breakpoints.reduce((acc, breakpoint) => {
acc[breakpoint] = { ...defaultValue };
return acc;
}, {} as { [P in K]: T });
};
版权声明:本文标题:typescript - How to generate generic type that allows to set partial defaults to given type? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308599a1933716.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
defaultSetting
really aSetting
? Where's it'sprop
prop?). Also it seems you're asking two separate questions (two different errors happening in two different places). Please edit this to ask a single primary question, or it risks being closed with "Needs more focus: This question currently includes multiple questions in one. It should focus on one problem only." – jcalz Commented Nov 21, 2024 at 17:34