ionic UI(3)TypeScript - handbook

本文介绍了TypeScript的基本数据类型,包括布尔型、数值型、字符串型等,并详细阐述了数组、枚举、任意类型及接口的应用。同时,文中还探讨了类的概念,包括继承、修饰符等高级特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ionic UI(3)TypeScript - handbook

1 Basic Types
boolean - var isDone :boolean = false;
number - var height: number = 6;
string - var name:string = ‘carl’;
name = “sillycat”;
Array - var list:number[] = [1,2,3];
var list:Array<number> = [1,2,3];
Enum - enum Color { Red, Green, Blue };
var c: Color = Color.Green;
usually, enum begin from 0, we can set the number as well.
enum Color {Red = 1, Green = 2, Blue = 4};
var colorName: string = Color[2];
alert(colorName);

The alert message is ‘Green'
Any - var notSure: any = 4;
notSure = “may be a string instead”;
notSure = false;
Void - function warnUser(): void {
alert(“warning message.");
}

2 Interfaces
interface LabelledValue {
label: string;
}

This interface requires there is a property label in the parameter object.

Optional Properties in Interface
interface SquareConfig {
color?: string;
width?: number;
}

Class Types
interface ClockInterface {
currentTime:Date;
setTime(d:Date);
}

class Clock implements ClockInterface {
currentTime: Date;
setTime(d:Date) {
this.currentTime = d;
}
constructor(h:number, m: number) { }
}

Extending Interfaces
interface Shape {
color: string;
}

interface Square extends Shape {
sideLength: number;
}
var square = <Square>{};
square.color = “blue”;
square.sideLength = 10;

3 Classes
Inheritance of Class
class Animal {
name: string;
contractor(theName:string) { this.name = theName; }
move(meters: number = 0) {
alert(this.name + “ can move “ + meters + “m.")
}
}

class Snake extends Animal {
constructor(name:string) { super(name); }
move(meters = 5) {
alert(“slithering….");
super.move(meters);
}
}

Private/Public modifiers - public is always the default

Getter / Setter
var passcode = “secret passcode”;

class Employee {
private _fullName: string;

get fullName(): string {
return this._fullName;
}

set fullName(newName: string) {
if(passcode && passcode == “secret passcode”){
this._fullName = newName;
}else{
alert(“Unauthorized update of employee!");
}
}
}

var employee = new Employee();
employee.fullName = “Carl Luo”;
if(employee.fullName) {
alert(employee.fullName);
}

4 Modules
>tsc —out sample.js Validation.ts LettersOnlyValidator.ts ZipCodeValidator.ts Test.ts

5 Functions
default and optional parameters
function buildName(firstName:string, lastName?:string){
}

function buildName(firstName:string, lastName = “Smith”) {
}

6 Generics

More documents in the handbook, but I think I am ok that I only read the first few chapters.
http://www.typescriptlang.org/Handbook

References:
http://www.typescriptlang.org/Handbook
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值