数据类型
let isDone: boolean = false;
let age: number = 22;
let myName: string = 'lq';
any类型
let user: any;
user.print();
let student: unknown;
student = 100;
student.print(); // error
let sentence: string = `My name is ${myName} I'll be ${ age + 1 } next year `
let ageList: number[] = [1, 2, 3]
let ageList1: Array
= [1, 2, 3] // 元组 tuple 元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同let tup: [string, number]tup = ['hello', 22]
// 枚举 enum类型,枚举类型可以为一组数值赋予友好的名字enum Color { Red, Green, Blue}let c: Color = Color.Green enum Red { DarkRed = 1, LightRed = 4} let redName: string = Red[4] function warn (): void { console.log('nothing')} function print (person: string) { return 'Hello ' + person} // 接口interface Person { firstName: string lastName: string} const user = { firstName: 'L', lastName: 'Q'} function hello (person: Person) { return 'Hello ' + person.firstName} const result = hello(user) class Student { public fullName: string constructor (public firstName, public lastName) { this.fullName = firstName + lastName }} const xiaoming = new Student('xiao', 'ming') function greet (person: Person) { return 'Hello ' + person.firstName + person.lastName} greet(xiaoming)
数据类型
-
类型注解: 在初始化变量定义变量类型,函数形参类型,函数返回值类型
-
接口 接口也是一种类型,可以将变量,参数,返回值这些数据定义成这种类型,表示该值包含了接口要求的结构
let myName: string = 'lq';
let age: number = 18;
let hasDone: boolean = false;
let values: number[] = [10, 20, 30];
枚举
enum Color {
RED,
Blue = 10,
White,
Black = '100'
}
let myColor: Color = Color.Black;
let sentence: string = My name is ${myName} I'll be ${ age + 1 } next year
let ageList: number[] = [1, 2, 3]let ageList1: Array = [1, 2, 3]
// 元组 tuple 元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同let tup: [string, number]tup = ['hello', 22]
// 枚举 enum类型,枚举类型可以为一组数值赋予友好的名字enum Color { Red, Green, Blue}let c: Color = Color.Green
enum Red { DarkRed = 1, LightRed = 4}
let redName: string = Red[4]
function warn (): void { console.log('nothing')}
function print (person: string) { return 'Hello ' + person}
// 接口interface Person { firstName: string lastName: string}
const user = { firstName: 'L', lastName: 'Q'}
function hello (person: Person) { return 'Hello ' + person.firstName}
const result = hello(user)
class Student { public fullName: string constructor (public firstName, public lastName) { this.fullName = firstName + lastName }}
const xiaoming = new Student('xiao', 'ming')
function greet (person: Person) { return 'Hello ' + person.firstName + person.lastName}
greet(xiaoming)
作者:hy__ 链接:juejin.cn/post/684490… 来源:掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
// tuple
let t: [string, number];
t = ['lq', 10];
t[1].toFixed(2);
let a: undefined;
a = undefined;
let b: null;
b = null;
let c: void = null; c = undefined; // void 表示没有任何类型 function test(): void { }
function test1(): never { throw new Error('hello error') }
function test2(): never { while (true) { } }
type Foo = string | number; // type Foo = string | number | boolean;
function test3(foo: Foo): void { if (typeof foo === 'number') {
} else if (typeof foo === 'string') {
} else {
let val: never = foo;
}
}
// 类型断言 let someValue: unknown = 'my name';
(someValue).length;
(someValue as string).length;
类型守卫
interface MyType {
name: string;
value: number;
}
interface YourType {
name: string;
age: number;
}
type otherType = MyType | YourType;
function test4(myValue: otherType): void {
console.log(myValue.name);
if ('value' in myValue) {
myValue.value;
}
if ('age' in myValue) {
myValue.age;
}
}
- typeof let s: symbol = Symbol();
typeof s;
// instanceof
let f = () => { }
f instanceof Object;
// 联合类型 function test5(value: number | boolean | string | symbol | undefined) { return value; }
test5(undefined);
// 类型别名 type FooType = string | number;
// 交叉类型 interface User { name: string; age: number; }
interface Student { height: number; weight: number; }
type Me = User & Student;
const me: Me = { name: 'lq', age: 23, height: 200, weight: 100 }
// 函数 function print(name: string): number { return name.length; }
let myPrint: (name: string) => number;
myPrint = function (x) { return x.length; }
function print2(name: string = 'lq', id?: number) { console.log(name) console.log(id) }
print2(); print2('lq') print2('lq', 100)
// 剩余参数 function print3(name, ...items) { items.forEach(item => { console.log(name + ' - ' + item) }) }
// 函数重载 function print(name: string): string; function print(age: number): number; function print(name: string): number; function print(x) { return x; }
// 方法重载 class Person {
add(name: string): string;
add(age: number): number;
add(x) {
return x;
}
}
print(123);
数组、对象解构、展开运算符
// 数组遍历
for (let i of [1, 2, 3]) {
console.log(i)
}
接口
interface Per {
name: string;
age: number;
readonly score: number;
school?: string;
}
let person: Per = {
name: 'lq',
age: 23,
score: 100,
}
类
泛型
let values1: Array<number> = [10, 20, 30];
// ReadonlyArray