在同一天做多种语言的开发工作并不少见。
那个时候,我经常不知道怎么写,所以每次都是边研究边工作。
为了减少研究的时间,我想以个人备忘录的形式总结一下。
不应触碰的内容
- 索引.d.ts
- @types
- 界面更好还是打字更好?
- 如何编写JavaScript及基础知识
- 详细的逻辑和解释
参考网站
基本用法
let 変数名: 型; let 配列変数名: 型[]; class クラス名 { 変数名: 型; } const func = (引数名: 引数型): 戻り値型 => { return 戻り値; };限制处理的IN/OUT,内部使用
function hello(name: string, helloAfterAction: (name: string) => number) { console.log(`hello ${name}`); helloAfterAction(name); } // interfaceのように定義する type HogeInterface = (hoge: string) => number; type FugaInterface = { (hoge: string): number; }; function hogee(name; string, hogeAction: HogeInterface) { console.log(`hogee ${name}`); hogeAction(name); }甲或乙
联合类型
let hoge: A | B;数组的内容是 A 或 B
let hoge: (A | B)[];一个或没有值(未定义)
可选的
class Hoge { hoge?: A; } class Fuga { hoge: A | undefined; } // A or undefined だけど、明示的な値定義を強制する class Piyo { hoge?: A | undefined; }默认值设置
function hello(name?: string) { name ??= 'ほげ'; } function hello(name: string = "ほげ") { }即使没有值(null 或 undefined)也可以安全使用
可选链
空合并运算符let hoge: undefined; let typeVal: string | undefined = hoge?.title?.type; let typeNumber: number = typeVal?.indexOf("ほ") ?? 0;什么是A和B
交叉口类型
type Hoge = A & B; function extend<A, B>(first: A, second: B): A & B { return { ...first, ...second }; }* 如果创建了无法识别的类型,则将其视为 never 类型。
使用时确定的具体类型
仿制药
function hello<T>(arg: T): string { return `hello ${arg}!`; } class Hoge<T> { private name: string; private hoge: T; constructor(name: string, hoge: T) { this.name = name; this.hoge = hoge; } } // 利用時 hello<string>("hoge"); new Hoge<number>("hoge", 1);如何定义类型
做一个模具
type Hoge = { name: string; };使用实体创建类型
class Hoge { hoge: string; }创建可以强制行为的类型
interface Hoge { hoge: string; }限制可用的值
只接受“hoge”或“fuga”
文字类型
const hello = (name: "hoge" | "fuga") => { console.log(`hello ${name}`); };* string , number , boolean 可以限制为实际可以使用的值
仅接受固定值
枚举
enum Username { Hoge = "hoge", Fuga = "fuga", } namespace Username { /** Username.hello(Username.Hoge); ができるようになる */ export function hello(name: Username): string { return `hello ${name}`; } }* 类似枚举的接口
interface ValueType; const Enum = { key: { ... } as ValueType, } as const; type Enum = typeof Enum[keyof typeof Enum];* 可以像枚举一样使用,也可以定义为字符串
function stringToEnum<T extends string>(o: T[]): { [K in T]: K } { return o.reduce((accumulator, currentValue) => { accumulator[currentValue] = currentValue; return accumulator; }, Object.create(null)); } const Hoge = stringToEnum(["hoge", "fuga"]); type Hoge = keyof typeof Hoge;*https://basarat.gitbook.io/typescript/type-system/literal-types#string-based-enums引用
接受相同类型的倍数
可变长度参数/剩余参数
function hello(name: string, ...greetedName: string[]) { }类型确定方法
在原始级别检查
let isString: boolean = typeof "" === 'string';类型 判断字符串 细绳 '细绳' 布尔值 '布尔' 数值 '数字' 不明确的 '不明确的' 无效的 '目的' 大整数 '大神' 功能 '功能' 象征 '象征' 其他 '目的' 粗略检查继承关系
let val: Hoge = ...; let isHoge: boolean = val instanceof Hoge; let isNotHoge: boolean = !(val instanceof Hoge);属性存在检查
let hasName: boolean = 'name' in val;检查过程后确定类型并使用
class Hoge { static isType(arg: any): arg as Hoge { let isNotNullObj: boolean = (arg === 'object' && arg !== null); return isNotNullObj && typeof (arg as Hoge).name === 'string'; }; } let val = ...; if (Hoge.isType(val)) { // このスコープ内では、val を Hoge として扱うことができる }用于交换数据的类型
在类中定义
class Hoge { hoge: string; constructor(init?: Partial<Hoge>) { Object.assign(this, init); } } (() => { let hoge: Hoge = new Hoge({ hoge: "fuga" }); })();在接口中定义
interface Hoge { hoge: string; } (() => { let hoge: Hoge = { hoge: "fuga" }; })();定义在一次性的前提下
内联类型注解
(() => { let hoge: { hoge: string; } = { hoge: "fuga" }; })();多种返回类型(使用数组)
元组
let [id, name, type]: [string, number, string] = ((val: string): [string, number, string] => { return [0, "hoge", "fuga"]; })("");多种返回类型
解构
let {id, name, type} = ((val: string): { id: number, name: string, type: string } => { return { id: 0, name: "hoge", type: "fuga", }; })("");在最后
有很多场景,当你有一定的经验时,你会忘记基本的部分。可以轻松检查,但我想尽可能缩短时间。
有可能会增加新的符号,所以这次总结的内容中可能存在一些不充分的部分。
如果您能告诉我是否有短缺或其他好的定义方法,我将不胜感激。
原创声明:本文系作者授权爱码网发表,未经许可,不得转载;
原文地址:https://www.likecs.com/show-308632574.html