使用 '@mui/material/styles' 中的样式函数时如何处理 Typescript 泛型
How to handle Typescript Generics when using styled function from '@mui/material/styles'
import Table,{TableProps} from 'my/table/path'
const StyledTable = styled(Table)({
...my styles
})
const AnotherTable = <T, H>(props: TableProps<T, H>) => {
return <StyledTable {...props} />
}
这是我的错误信息:
Types of parameters 'item' and 'item' are incompatible.
Type 'unknown' is not assignable to type 'T'.
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'.
似乎我的泛型在使用 styled
时没有被继承,因为当我这样做时:
const AnotherTable = <T, H>(props: TableProps<T, H>) => {
return <Table {...props} />
}
它没有 return 任何错误,我的泛型正在工作
我解决了。基本上我所做的就是在创建后显式推断类型。
const StyledTable = styled(Table)({
...my styles
}) as typeof Table;
import Table,{TableProps} from 'my/table/path'
const StyledTable = styled(Table)({
...my styles
})
const AnotherTable = <T, H>(props: TableProps<T, H>) => {
return <StyledTable {...props} />
}
这是我的错误信息:
Types of parameters 'item' and 'item' are incompatible.
Type 'unknown' is not assignable to type 'T'.
'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'unknown'.
似乎我的泛型在使用 styled
时没有被继承,因为当我这样做时:
const AnotherTable = <T, H>(props: TableProps<T, H>) => {
return <Table {...props} />
}
它没有 return 任何错误,我的泛型正在工作
我解决了。基本上我所做的就是在创建后显式推断类型。
const StyledTable = styled(Table)({
...my styles
}) as typeof Table;