我是打字稿新手。我使用 ant design 4.0 版,我想向我的列添加对齐属性,但在表的 columns 属性中它显示此错误。我知道我应该将变量的类型与 AlignType 相匹配,但我不知道具体该怎么做。提前谢谢你们了。
interface ColumnSharedType<RecordType> {
title?: React.ReactNode;
key?: Key;
className?: string;
fixed?: FixedType;
onHeaderCell?: GetComponentProps<ColumnsType<RecordType>[number]>;
ellipsis?: boolean;
align?: AlignType;
}
export interface ColumnGroupType<RecordType> extends ColumnSharedType<RecordType> {
children: ColumnsType<RecordType>;
}
export declare type AlignType = 'left' | 'center' | 'right';
const columns = [
{
title: 'State',
dataIndex: 'state',
render: (text: any) => <a>{text}</a>,
align: 'left', // it need type AlignType
},
]
return (
<Table className="state__table" columns={columns} dataSource={data}/>
// TS2322 Type 'string' is not assignable to type '"left" | "center" | "right" | undefined'.
)
ps:我添加新变量
const right: AlignType = 'right';
const columns = [
{
title: 'State',
dataIndex: 'state',
render: (text: any) => <a>{text}</a>,
align: right,
},
]
它有效,但我不确定这是一个好的解决方案。