Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table组件使用fixed属性报错 #3772

Closed
omnip620 opened this issue Nov 9, 2016 · 13 comments
Closed

Table组件使用fixed属性报错 #3772

omnip620 opened this issue Nov 9, 2016 · 13 comments

Comments

@omnip620
Copy link

omnip620 commented Nov 9, 2016

发生问题的环境是:

  • antd 版本:2.3.1
  • typescript版本:2.1.1

按照官网提供的示例代码

columns=[
...
 {
    title:'操作',
    key:'operation',
    fixed: 'right',
    width: 200
 }
...
]

编译过程报错
Type 'string' is not assignable to type 'boolean | "left" | "right"'.

除非写成 fixed:'right' as 'right'

期待的结果是:

怎么能像官网一样的写法不报错

@yesmeck
Copy link
Member

yesmeck commented Nov 9, 2016

好像是 typescript 的正常行为。

@afc163
Copy link
Member

afc163 commented Nov 9, 2016

。。。这么变态

@yesmeck
Copy link
Member

yesmeck commented Nov 9, 2016

type Foo = boolean | 'left' | 'right';

let foo = 'right';
const bar: Foo = foo;

这里 foo 的类型是 string,不能直接 assign 给 bar

@infeng
Copy link
Contributor

infeng commented Nov 9, 2016

我这边不会提示这个错误

@yesmeck
Copy link
Member

yesmeck commented Nov 9, 2016

@infeng 你 typescript 是什么版本?

@infeng
Copy link
Contributor

infeng commented Nov 9, 2016

2.0.8

@yesmeck
Copy link
Member

yesmeck commented Nov 9, 2016

@infeng 我用 2.0.8 也有这个错误。。。

@infeng
Copy link
Contributor

infeng commented Nov 9, 2016

原来我看错了,也是有这个错误。。。

@infeng
Copy link
Contributor

infeng commented Nov 9, 2016

import { TableColumnConfig } from 'antd/lib/table/Table';
let columns: TableColumnConfig[] = [{
  title: 'name',
  fixed: 'right',
}];

这样写可以省点功夫,不用每个fixed都写asO(∩_∩)O

@yesmeck
Copy link
Member

yesmeck commented Nov 9, 2016

@infeng TableColumnConfig[]这样会报错吧,TableColumnConfig 需要一个 generic type 的。

import React, { Component } from 'react';
import { Table } from 'antd';
import { TableColumnConfig } from 'antd/lib/table/Table';

interface Person {
  name: string;
}

const columns: TableColumnConfig<Person>[] = [{
  title: 'Name',
  dataIndex: 'name',
  fixed: 'right',
}];

const data: Person[] = [{
  name: 'Jack',
}];

class PersonTable extends Table<Person> {}

class App extends Component<null, null> {
  render() {
    return (
      <PersonTable columns={columns} dataSource={data} />
    )
  }
}

@infeng
Copy link
Contributor

infeng commented Nov 9, 2016

对,最新版的是要加generic,我忘记我用的antd还是2.1.0,还没有加上generic

@omnip620
Copy link
Author

omnip620 commented Nov 9, 2016

@infeng @yesmeck 偷了个懒
columns: TableColumnConfig<any>[]=...

@yesmeck yesmeck closed this as completed Nov 9, 2016
@lock
Copy link

lock bot commented May 3, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators May 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants