• 周五. 3月 29th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

TypeScript总结(一)

admin

11月 28, 2021

一、数据类型

boolean、number、string、数组(number[]、Array<number>)、元组Tuple(限定元素数量和类型的数组)(let x: [string, number])、枚举(enum Color {Red = 1, Green, Blue})、Any(let notSure: any = 4)、Void(function warnUser(): void {})(let unusable: void = undefined;)(变量void类型只能null或undefined)、Null和Undefined、Never(。。。)、Object(。。。)

二、类型断言

let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;// 1.<>
let strLength: number = (someValue as string).length;// 2.as

  let和var

1 for (let i = 0; i < 10 ; i++) {
2     setTimeout(function() {console.log(i); }, 100 * i);
3 }

输出0到9

1 for (var i = 0; i < 10; i++) {
2     setTimeout(function() { console.log(i); }, 100 * i);
3 }

输出10个10

解构赋值(数组、对象,…xxx)(。。。)

三、接口interface(相当于自定义的一种数据类型)

可选属性?,只读属性readonly(ReadonlyArray<T>)、函数类型(。。。)、可索引类型,继承extends(可多个)、混合类型(。。。)

1 interface SquareConfig {
2     color?: string;
3     readonly  number;
4 }

四、类class

共有、私有、保护(public、private、protected),readonly,constructor,存取器getters/setters(get、set,内部方法修饰符),静态属性static,抽象类abstract(abstractclass),构造函数(。。。)

实现接口

1 interface ClockInterface {
2     currentTime: Date;
3 }
4 
5 class Clock implements ClockInterface {
6     currentTime: Date;
7     constructor(h: number, m: number) { }
8 }

接口继承类

 1 class Control {
 2     private state: any;
 3 }
 4 
 5 interface SelectableControl extends Control {
 6     select(): void;
 7 }
 8 
 9 class Button extends Control implements SelectableControl {
10     select() { }
11 }
12 
13 class TextBox extends Control {
14     select() { }
15 }

其他

环境:tsc编译命令报安全策略错误,修改方法:(以管理员运行)命令行,输入set-ExecutionPolicy RemoteSigned(如果无法以管理员运行,可加上“ CurrentUser”来只修改当前用户的安全策略,或者根据提示找到对应的注册表直接修改应该也可以,暂时没试)

元组 Tuple

FIGHTING

《TypeScript总结(一)》有一个想法
  1. Wow, marvelous blog layout! How lengthy have you
    ever been running a blog for? you made running a blog look easy.
    The full look of your site is fantastic, as smartly
    as the content material! You can see similar here sklep internetowy

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注