如何在 React with Typescript 中使用 Immutable.js 对象列表?
How do I use an Immutable.js List of objects in React with Typescript?
我正在通过编写单人纸牌游戏来学习 React 和 Typescript。
来自 List
的不可变文档:创建一个新的不可变列表,其中包含所提供的类似集合的值。 List<T>(): List<T>
我看到很多扩展 Map
等的示例,但找不到如何将 List
对象与 Typescript 一起使用。
如何在接口中声明类型,如何在状态中创建列表?
import React from 'react';
import './Game.scss';
import Card from '../card/Card'
const { List } = require('immutable');
interface IState {
deck: List<Card>; // Doesn't compile... "typeof List" does compile, but isn't typesafe?
}
class Game extends React.Component {
readonly state = {
deck: List<Card>() // Also doesn't compile... "List()" does compile, but isn't typesafe?
}
render () {
return ...
}
}
错误是:
TypeScript error in /Volumes/User/dev/games/spider/src/modules/game/Game.tsx(24,9):
'List' refers to a value, but is being used as a type here. Did you mean 'typeof List'? TS2749
> 24 | deck: List<Card>;
| ^
25 | }
从https://github.com/Microsoft/TypeScript/issues/1634开始,问题是必须导入不可变库,而不仅仅是必需的。
import React from 'react';
import { List } from 'immutable';
import './Game.scss';
import Card from '../card/Card'
interface IState {
deck: List<typeof Card>;
}
class Game extends React.Component {
readonly state: IState = {
deck: List<typeof Card>(),
};
我正在通过编写单人纸牌游戏来学习 React 和 Typescript。
来自 List
的不可变文档:创建一个新的不可变列表,其中包含所提供的类似集合的值。 List<T>(): List<T>
我看到很多扩展 Map
等的示例,但找不到如何将 List
对象与 Typescript 一起使用。
如何在接口中声明类型,如何在状态中创建列表?
import React from 'react';
import './Game.scss';
import Card from '../card/Card'
const { List } = require('immutable');
interface IState {
deck: List<Card>; // Doesn't compile... "typeof List" does compile, but isn't typesafe?
}
class Game extends React.Component {
readonly state = {
deck: List<Card>() // Also doesn't compile... "List()" does compile, but isn't typesafe?
}
render () {
return ...
}
}
错误是:
TypeScript error in /Volumes/User/dev/games/spider/src/modules/game/Game.tsx(24,9):
'List' refers to a value, but is being used as a type here. Did you mean 'typeof List'? TS2749
> 24 | deck: List<Card>;
| ^
25 | }
从https://github.com/Microsoft/TypeScript/issues/1634开始,问题是必须导入不可变库,而不仅仅是必需的。
import React from 'react';
import { List } from 'immutable';
import './Game.scss';
import Card from '../card/Card'
interface IState {
deck: List<typeof Card>;
}
class Game extends React.Component {
readonly state: IState = {
deck: List<typeof Card>(),
};