带有打字稿的 antd:列 align='right' 的表未编译

Pri*_*cis 7 typescript reactjs antd

我正在使用 antd Table 和 Typescript,如下所示

<Table dataSource={data} columns={columns2} />
Run Code Online (Sandbox Code Playgroud)

当我给align: 'right'其中一列时,它没有编译。显示以下错误。我无法弄清楚这些问题的根本原因。任何帮助表示赞赏。

Types of property 'align' are incompatible.
          Type 'string' is not assignable to type '"right" | "left" | "center" | undefined'.  TS2322

    47 |   public render() {
    48 |     return (
  > 49 |       <Table dataSource={data} columns={columns2} />
       |                                ^
    50 |     );
    51 |   }
    52 | }
Run Code Online (Sandbox Code Playgroud)

完整代码是

<Table dataSource={data} columns={columns2} />
Run Code Online (Sandbox Code Playgroud)

小智 6

import { AlignType } from 'rc-table/lib/interface';

align: 'right' as 'right',// not recommend
align: 'right' as const,// ok
align: 'right' as AlignType,// good
Run Code Online (Sandbox Code Playgroud)

3 种方法来解决您的问题。


小智 5

这对我有用。

align: 'right' as const,
Run Code Online (Sandbox Code Playgroud)

所以代码看起来像这样:

{
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      align: 'right' as const,
}
Run Code Online (Sandbox Code Playgroud)


小智 3

试试这个。它对我有用

{
      title: 'Age',
      dataIndex: 'age',
      key: 'age',
      align: 'right' as 'right',
}
Run Code Online (Sandbox Code Playgroud)