文章目录
ts
类型注解
普通类型
let str: string
str = 'abc'
function fn(msg: string): void {
alert(msg)
}
fn(str)
// 永远不存在的值的类型
function thr(msg: string): never {
throw new Error(msg)
}
thr('未知错误')
只读数组
let arr: ReadonlyArray<number> = [1,2,3]
arr.unshift(0)
函数类型
// 1.普通函数
let f1: Function
f1 = function(): void {
console.log('f1');
}
f1()
// 2.箭头函数
let f2: ()=>void;
f2 = () => {
console.log('f2');
}
f2()
// 类型别名
type f = () => void
let f3: f = function() {
console.log('f3');
}
f3()
// 4.接口
interface IFn {
(): void;
}
let fn: IFn = function() {
console.log("fn: IFn");
};
fn();
interface IFun {
(a: number, b: number): boolean;
}
let fun: IFun = function(a: number, b: number): boolean {
return a > b ? true : false;
};
console.log(fun(2,1));
枚举
enum Sex {
man= 1,
women
}
let s: Sex = Sex.man
类型断言
function fn(str: number|string): number {
return str.length
}
// 方式一
function fn(str: number|string): number {
if((<string>str).length) {
return (<string>str).length
} else {
return str.toString().length
}
}
// 方式二
function fn(str: number|string): number {
if((str as string).length) {
return (str as string).length
} else {
return str.toString().length
}
}
接口
interface IPerson {
name: string
age: number
}
let p1: person = {
name: 'zs',
age: 18
}
class Person {
name: string
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
// class 还可以这么写
interface IP {
name: string
age: number
}
class P {
constructor(public name: string, public age: number) {
this.name = name
this.age = age
}
}
let p: P = new P('zs', 20)
console.log(p);
接口–可选属性
enum Sex {
man= 1,
women
}
interface IPerson {
name: string;
age: number;
sex: Sex;
}
class Person {
name: string;
age: number;
sex: Sex;
constructor(name: string, age: number, sex: Sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
}
let p: Person = new Person('zs', 20, Sex.women)
console.log(p);
接口–只读属性
enum Sex {
women,
man
}
interface Person {
name: string
readonly sex: Sex
}
let p: Person = {
name: 'zs',
sex: Sex.women
}
console.log(p,Sex[p.sex]);
额外的属性检查
类(class)类型 类型检查不会检查私有类型和静态部分
interface IPerson {
name: string
age: number
[propname: string]: any
}
let p: IPerson = {
name: 'zs',
age: 20,
say(){
console.log(this.name)
}
}
p.say()
接口的继承
interface Fullname {
firstname: string
lastname: string
}
interface Person extends Fullname {
age: number
sex?: string
}
let p: Person = {
firstname: 'z',
lastname: 's',
age: 20
}
p.sex = 'man'
console.log(p)
class
修饰符
interface a{
a: string
}
interface b extends a {
c: string
}
class P implements b {
public a: string
private b: string
public c: string
protected d: string
static e: number = 1
constructor(a: string,c: string,b: string,d: string) {
this.a = a
this.b = b
this.c = c
this.d = d
}
}
let s = new P('1','2','3','4')
console.log(s,P.e);
存取器 和 静态属性
let str: string = 'denglu999'
interface IP {
}
class P {
private _age: number
static pwd: string = 'denglu999'
constructor(age: number) {
this._age = age
}
public get age(): number {
return this._age
}
public set age(age: number) {
if(this.checkPwd(str))
this._age = age
else
console.log('密码错误');
}
checkPwd(str: string): boolean {
return str===P.pwd
}
}
let p: P = new P(20)
console.log(p.age);
p.age = 18
console.log(p.age);
抽象
抽象类
abstract class AP {
public name: string
constructor(name: string) {
this.name = name
}
abstract say(): void
}
class P extends AP {
public age: number
constructor(name: string,age: number) {
super(name)
this.age = age
}
say() {
console.log(`my name is ${this.name}`);
}
}
let p: P = new P('zs', 20)
console.log(p);
p.say()
泛型
// 类型变量
function fn<T>(a: T): T {
return a
}
console.log(fn({name: 'zs'}));
console.log(fn(123));
// 扩展
function fn<T>(a: T): T[] {
return [a]
}
console.log(fn({name: 'zs'}));
泛型类
class P<T> {
public a: T
constructor(a: T) {
this.a = a
}
log(): T{
return this.a
}
}
let p = new P(1)
console.log(p.log());
let p1 = new P('1')
console.log(p1.log());
泛型约束
interface ILength {
length: number
}
function fn<T extends ILength>(a: T): T {
return a
}
console.log(fn(1)); // Argument of type '1' is not assignable to parameter of type 'ILength'.
console.log(fn([1]));
泛型约束中使用类型参数
// keyof : 索引类型查询操作符
// K extends keyof T: 查询T中所有的key条件限制
// K 的类型属于IT的属性名之一
interface IT {
name: string
age: number
}
function fn<IT, K extends keyof IT>(obj: IT,key: K): any {
return obj[key]
}
let p: IT = {
name: 'zs',
age: 20
}
// console.log(fn(p,'sex'));
console.log(fn(p,'name'));