首页 > 解决方案 > 学习一成不变,听不懂他们说什么

问题描述

我正在研究 immutable.js 的记录。

但是这段代码几乎要了我的命。

我的问题

  1. 我知道 [import, export,const] 但 [type] 是什么意思。

  2. defaultValues:, makePoint3D:, getName(): string,setName(name: string): this什么意思。:除非在 object 或 if 中,否则我从未见过。

这个问题是我理解的关键。

请给我建议!

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);
  }
}

来自https://immutable-js.github.io/immutable-js/docs/#/Record

标签: javascripttypescriptrecordimmutable.js

解决方案


getName(): string ,getName()是你的方法和部分后:告诉返回类型。基本上后面的部分:通常表示关于 type 。返回类型,变量类型等,无论您使用什么

getName() : string
{
return "Hi";       
}

getName() : string
{
 let a = 2;
 return a;
}

第二种方法会给出error,因为我们的方法返回类型是string并且我们正在返回一个Number

有时我们在像:这样的if条件下使用它

 isGendeMale : boolean = false;
 var gender : string = isGendeMale ? "Male" : "Female";

解释

?true如果条件为, 则检查值,如果分配之后的部分,则:分配之前的部分false:

冒号与等号

基本类型


推荐阅读