void和never和any区别?
any:任意类型;
void: 表示没有任何类型,常用于函数的返回值类型 () => void
;
never: 永远不存在的类型,比如 throw new Error('wrong')
,或者根本没有返回值时;
泛型是什么?
定义:允许在程序中定义形式类型参数,然后在泛型实例化时使用实际类型参数来替换形式类型参数。
作用:实现组件复用
function identity<T>(arg: T): T {
return arg;
}
const foo = identity<string>('222'); // 也可以这样: const foo = identity('222'); 能够自动推断出字符串类型
const foo = identity<number>(222);
type和interface的区别
- type能够用来表示非对象类型,而接口则只能表示对象类型;
- interface可以继承其他的接口、类,而type则不支持继承。type要实现类似继承的功能,可以借助交叉类型。 ```typescript interface Shape { name: string; } interface Circle extends Shape { radius: number; }
type Shape = {name: string};
type Circle = Shape & {radius: number};
3. interface具有**声明合并**的行为,而类型别名则不会进行声明合并。
```typescript
interface A {
x: number;
}
interface A {
y: number;
}
// 相当于
interface A {
x: number;
y: number;
}
交叉类型和联合类型
交叉类型:表示一个值同属于多个类型,用“&”来表示;
interface Clickable {
click(): void;
}
interface Focusable {
focus(): void;
}
type T = Clickable & Focusable;
interface Clickable {
register(x: any): void;
}
interface Focusable {
register(x: string): boolean;
}
type ClickableAndFocusable = Clickable & Focusable;
type FocusableAndFocusable = Focusable & Clickable;
function foo(
clickFocus: ClickableAndFocusable,
focusClick: FocusableAndFocusable,
) {
// 当交叉类型碰到函数重载时,与成员定义顺序一样,即使用了Clickable
let a: void = clickFocus.register('foo');
// 使用了 Focusable
let b: boolean = focusClick.register('foo');
}
联合类型:表示一个值的类型为多种类型之一,用“|”来表示;
交叉类型和联合类型的优先级:
当表示交叉类型的“&”符号与表示联合类型的“|”符号同时使用时,“&”符号具有更高的优先级。“&”符号如同数学中的乘法符号“×”,而“|”符号则如同数学中的加法符号“+”。
A & B | C & D // 相当于(A & B) | (C & D)
() => bigint | number // 相当于 () =>(bigint | number)
协变、逆变、双变
协变:设有两个复杂类型Complex(A)和Complex(B),如果由A是B的子类型能够得出Complex(A)是Complex(B)的子类型,那么我们将这种变型称作协变。A <: B → Complex(A) <: Complex(B)
interface Animal {
age: number;
}
interface Dog extends Animal {
bark(): void;
}
let animal: Animal = { age: 12 };
let dog: Dog = {
age: 12,
bark: () => {
console.log('bark');
},
};
animal = dog; // 兼容,能赋值成功,这就是一个协变
dog = animal; // 不兼容,会抛出类型错误:Property 'bark' is missing in type 'Animal' but required in type 'Dog'
逆变:如果由A是B的子类型能够得出Complex(B)是Complex(A)的子类型,那么我们将这种变型称作逆变。A <: B → Complex(B) <: Complex(A)
;
interface Animal {
age: number;
}
interface Dog extends Animal {
bark(): void;
}
let visitAnimal = (animal: Animal): Dog => {
animal.age;
return {
age: 12,
bark() {},
};
};
let visitDog = (dog: Dog): Animal => {
dog.age;
dog.bark();
return {
age: 20,
};
};
visitDog = visitAnimal; // 兼容
visitAnimal = visitDog; // 不兼容, 会抛出类型错误
双变:如果由A是B的子类型或者B是A的子类型能够得出Complex(A)是Complex(B)的子类型,那么我们将这种变型称作双变。A <: B 或 B <: A → Complex(A) <: Complex(B)
函数重载
function add(x: number, y: number): number;
function add(x: any[], y: any[]): any[];
function add(x: number | any[], y: number | any[]): any {
// 省略了实现代码
}