TypeScript基本概念

1. 基础类型

有12种

布尔值 let isDone: boolean = false;

数字 let dec: number = 6;

字符串 let name : string = 'bob';

数组 let list: number[] = [1, 2, 3];

元组 let x : [string, number] = ['hello', 10]

枚举 enum Color {Red, Green, Blue} let c: Color = Color.Green;

Any let notSure: any = 4;

Void function warnUser(): void { console.log('this is no return value') }

Null/Undefined 对应于js中的null和undefined

Never 表示那些永不存在的值的类型

Object 对象类型

类型断言 强制类型转换 <string>someValue 或者 someValue as string

2. 接口

接口类似于一个结构体,可以用来定义 对象的属性类型,函数类型,类类型等。

interface SquareConfig {
    color?: string;
    width?: number;
}

interface SearchFunc {
    (source: stirng,subString: string): boolean;
}

2.1 接口继承

接口可以多继承

复制代码
interface Shape {
    color: string;
}

interface PenStroke {
    penWidth: number;
}

interface Square extends Shape, PenStroke {
    sideLength: number;
}

let square = <Square>{};
square.color = "blue";
square.sideLength = 10;
square.penWidth = 5.0;
复制代码

2.2 混合类型

一个类型可以既是函数,又是对象,掺杂类型。

复制代码
interface Counter{
    (start: number): string;
    interval: number;
    reset(): void;
}

function getCounter(): Counter {
    let counter = <Counter>function(start: number){ };
    counter.interval = 123;
    counter.reset = function(){};
    return counter;
}

let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;
复制代码

2.3 接口继承类

接口继承了一个类,只会继承该类的成员,但不会继承其实现。可以继承到该类的 private 和 protected 成员。

3. 类

与ES6的类相同,有一些额外的支持

3.1 成员修饰符(公有,私有 和 受保护)

private,protected,public

复制代码
class Person {
    protected name: string;
    constructor(name: string) { this.name = name; }
}

class Employee extends Person {
    private department: string;

    constructor(name: string, department: string) {
        super(name)
        this.department = department;
    }

    public getElevatorPitch() {
        return `Hello, my name is ${this.name} and I work in ${this.department}.`;
    }
}
复制代码

3.2 只读 readonly

class Octopus {
    readonly name: string;
    readonly numberOfLegs: number = 8;
    constructor (theName: string) {
        this.name = theName;
    }
}

3.3 抽象类

abstract class Animal {
    abstract makeSound(): void;
    move(): void {
        console.log('roaming the earch...');
    }
}

4. 函数

与ES6函数不同点有:

4.1 函数声明 参数和返回值 都带类型

function add(x: number, y: number): number {
    return x + y;
}

4.2 支持函数重载

function pickCard(x: {suit: string; card: number; }[]): number;

function pickCard(x: number): {suit: string; card: number; };

5. 泛型

用类型变量来约束类中的成员变量的各种类型,包括 属性类型,方法参数类型和返回值类型

class GenericNumber<T> {
    zeroValue: T;
    add: (x: T, y: T) => T;
}

let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) { return x + y; };

7. 高级类型

7.1 交叉类型

多个类型合并为一个类型,合成类型,包含了所有类型的特性。效果如:Person & Serializable & Loggable

7.2 联合类型

可以是多个类型中的任何一个类型,例如:number | string | boolean 表示一个值可以是 number, string,或 boolean。

若值是联合类型,只能访问联合类型所有类型里共有的成员。

复制代码
interface Bird {
    fly();
    layEggs();
}

interface Fish {
    swim();
    layEggs();
}

function getSmallPet(): Fish | Bird {
    // ...
}

let pet = getSmallPet();
pet.layEggs(); // okay
pet.swim();    // errors
复制代码

7.5 可为null类型

就是C#中的可空类型,b可以为null

class C {
    a: number;
    b?: number;
}

7.4 类型别名

类型别名就是给类型起一个新名字。别名不会创建新类型,只是引用了原来那个类型。

复制代码
type Name = string;
type NameResolver = () => string;
type NameOrResolver = Name | NameResolver;

type Tree<T> = {
    value: T;
    left: Tree<T>;
    right: Tree<T>;
}
复制代码

8. 命名空间

同C#命名空间

posted @   全玉  阅读(277)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-05-21 判断元素是否在视口和元素相交
2017-05-21 单链表的节点数,合并,相交,反转
2017-05-21 双向链表
2017-05-21 单链表的删除
点击右上角即可分享
微信分享提示