let isDone: boolean = false
let age: number = 20
let binaryNumber: number = 0b1111
binaryNumber.toString
let fristName: string = 'shanyao'
fristName.charCodeAt
let message: string = `hello, ${fristName}, age is ${age}`
let u: undefined = undefined
let n: null = null
let num:number = undefined
//代表任何类型 会丧失具体的类型作用
let notSure: any = 4
notSure = 'maybe it is a string'
notSure = true
notSure.myname
notSure.myname()
//联合类型
let numberOrString: number | string = 234
numberOrString = 'abc'
//数组
let arrOfNumbers: number[] = [1, 2, 3, 4]
arrOfNumbers.push(5)
//类数组
function tets() {
console.log(arguments);
arguments.length
arguments[2]
// 会报错
// arguments.forEach
let htmlCollection: Node
}
//元组
let user: [string, number] = ['shanyao', 1]
//Interface接口
// 对对象的形状(shape)进行描述
// 对类(class)进行抽象
// Duck Typing (鸭子类型)
interface Person {
readonly name: string;
age: number;
dateBirth: string;
}
let shanyao: Person = {
name: 'shanyao',
age: 20,
dateBirth: '2020-04-20'
}
//泛型
function echo<T>(arg: T): T {
return arg
}
const str: string = 'str'
const result1: string = echo(str)
function swap<T, U>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[0]]
}
const result2 = swap(['string', 123])
// result2[1].codePointAt
//约束泛型
function echoWithArr<T>(arg: T[]): T[] {
console.log(arg.length);
return arg
}
const arrs = echoWithArr([1, 2, 3])
//必须含有length的属性
interface IWithLength {
length: number
}
function echoWithLength<T extends IWithLength>(arg: T): T {
console.log(arg.length)
return arg
}
const str1 = echoWithLength('str1')
const obj = echoWithLength({length: 10, width: 20})
const arr2 = echoWithLength([1, 2, 3])
// const number2 = echoWithLength(123)
class Queue<T> {
private data = []
push(item: T){
return this.data.push(item)
}
pop(): T{
return this.data.shift()
}
}
const queue = new Queue<number>()
queue.push(1)
console.log(queue.pop().toFixed())
const queue2 = new Queue<string>()
queue2.push('12')
console.log(queue.pop.length);
interface KeyPair<T, U> {
key: T;
value: U;
}
let kp1: KeyPair<number, string> = { key: 123, value: '1212'}
let kp2: KeyPair<string, number> = { key: '12', value: 12}
let arr:number[] = [1, 2, 3]
let arrTwo: Array<number> = [1, 2, 3]
interface IPlus<T>{
(a: T, b: T): T
}
function plus(a: number, b: number): number {
return a + b
}
function connect(a:string, b:string) {
return a + b
}
const a: IPlus<number> = plus
const b: IPlus<string> = connect
// type aliaces 类型别名
type PlusType = (x: number, y: number) => number
function sum(x: number, y:number) {
return x + y
}
const sum2: PlusType = sum
type NameResolver = () => string
type NameOrResolver = string | NameResolver //等价于string | (x: number)=> string
function getName(n: NameOrResolver) {
if(typeof n === 'string'){
return n
} else {
return n()
}
}
//type assertion 类型断言 类型断言不是类型装换 联合类型不能断言成别的类型
//input as String 等价于 <string>input
function getLength(input: string | number): number {
// const str4 = input as String
// if(str.length){
// return str.length
// } else {
// const number = input as Number
// return number.toString().length
// }
if((<string>input).length) {
return (<string>input).length
}else{
return input.toString().length
}
}
//函数声明的写法
function add(x:number, y:number, z:number = 10): number {
if (typeof z === 'number') {
return x + y + z
}else{
return x + y
}
}
let result = add(2, 3)
const add2 = function (x: number, y: number, z: number = 10): number {
if (typeof z === 'number') {
return x + y + z
} else {
return x + y
}
}
const add3: (x: number, y: number, z?:number) => number = add2
//常量枚举 计算枚举
enum Direction {
Up = 'UP',
Down = 'DOWN',
Left = 'LEFT',
Right = 'RIGHT'
}
console.log(Direction.Up)
console.log(Direction[0])
const value = 'UP'
if (value === Direction.Up) {
console.log('go up!');
}
// 类(class): 定义一切事物的抽象特点
// 对象(object): 类的实例
// 面向对象(OOP): 三大特性:封装, 继承,
// 修饰符 public(公有的) private(私有的) protected(保护继承 父子可以访问) readonly(只读属性)
class Animal {
public name: string;
//静态方法 static
static categoies: string[] = ['mammal', 'bird']
static isAnimal(a) {
return a instanceof Animal
}
constructor(name: string) {
this.name = name
}
run() {
return `${this.name} is running`
}
}
const snake = new Animal('lily')
console.log(snake.run());
console.log(Animal.categoies);
console.log(Animal.isAnimal(snake));
class Dog extends Animal {
bark(){
return `${this.name} is barking`
}
}
const xiaobao = new Dog('xiaobao')
console.log(xiaobao.run());
console.log(xiaobao.bark());
class Cat extends Animal {
constructor(name){
super(name)
console.log(this.name);
}
run() {
return `Meow, ${super.run()}`
}
}
const maomao = new Cat('maomao')
console.log(maomao.run());
//共有特性
interface Radio {
switchRadio(): void
}
interface Battery {
checkBatteryStatus();
}
interface RadioWithBattery extends Radio {
checkBatteryStatus();
}
class Car implements Radio {
switchRadio(){
}
}
class Cellphone implements RadioWithBattery {
switchRadio() {
}
checkBatteryStatus() {
}
}
TypeScript学习笔记
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 今天青石的票圈出镜率最高的,莫过于张艺谋的新片终于定档了。 一张满溢着水墨风的海报一次次的出现在票圈里,也就是老谋...