【发布时间】:2020-10-21 10:28:44
【问题描述】:
我正在研究 immutable.js 的记录。
但这段代码几乎要了我的命。
我的问题
- 我知道 [import, export,const] 但 [type] 是什么意思。
-
defaultValues:、makePoint3D:、getName(): string、setName(name: string): this是什么意思。我从未见过:,除非在对象中或如果。
这个问题是我理解的关键。
请给我建议!
import type { RecordFactory, RecordOf } from 'immutable';
// Use RecordFactory<TProps> for defining new Record factory functions.
type Point3DProps = { x: number, y: number, z: number };
const defaultValues: Point3DProps = { x: 0, y: 0, z: 0 };
const makePoint3D: RecordFactory<Point3DProps> = Record(defaultValues);
export makePoint3D;
// Use RecordOf<T> for defining new instances of that Record.
export type Point3D = RecordOf<Point3DProps>;
const some3DPoint: Point3D = makePoint3D({ x: 10, y: 20, z: 30 });
type PersonProps = {name: string, age: number};
const defaultValues: PersonProps = {name: 'Aristotle', age: 2400};
const PersonRecord = Record(defaultValues);
class Person extends PersonRecord<PersonProps> {
getName(): string {
return this.get('name')
}
setName(name: string): this {
return this.set('name', name);
}
}
【问题讨论】:
-
getName(): string, getName() 是您的方法,:之后的部分讲述了返回类型。基本上:之后的部分通常表示大约type。返回类型,变量类型等无论你使用什么 -
这种奇葩方式只有js?
-
是的,我们在打字稿中以这种方式使用
标签: javascript typescript record immutable.js