2018 TypeScript Update(1)Introduction Basic Grammar - Types and Interface

本文介绍TypeScript的基础语法,包括基本类型、数组、元组、枚举等,并讲解变量声明、接口使用方法及类的基本概念。

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

2018 TypeScript Update(1)Introduction Basic Grammar - Types and Interface

Check the Current ENV
>node --version && npm --version
v8.0.0
5.6.0

Install TypeScript
>npm install -g typescript

Check Version
>tsc --version
Version 2.7.2

Language Related
http://www.typescriptlang.org/docs/handbook/basic-types.html
Basic Types
Boolean
let isDone: boolean = false;
Number
let decimal: number = 6;
let binary: number = 0b1010;
String
let color: string = “blue”;
let fullName: string = `Carl Luo`;
let age: number = 36;
let sentence: string = `Hello, my name is ${ fullName }.
I will be ${ age + 1} years old next year.`;

${} and `` in string are really useful.

Array
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];

Tuple
let x : [string, number];
x = [“hello”, 10];
// x[0] is “hello”
// x[1] is 10

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

enum Color {Red = 1, Green = 2, Blue = 4}

Any
let notSure: any = 4;
let list: any[] = [1, true, “free"];

Void
function warnUser(): void {
alert(“warning");
}

Null and Undefined
null undefined

Never
unreachable end point

Force the Type
let someValue: any = “this is a string”;
let strLength: number = (<string>someValue).length;
let strLength: number = (someValue as string).length;

http://www.typescriptlang.org/docs/handbook/variable-declarations.html
Variable Declarations
let and const

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

Interface
http://www.typescriptlang.org/docs/handbook/interfaces.html
interface LabelledValue {
label: string;
}

function printLabel(labelledObj: LabelledValue){
console.log(labelledObj.label);
}

let myObj = { size: 10, label: “size 10 object”};
printLabel(myObj);

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

Readonly Properties
interface Point {
readonly x: number;
readonly y: number;
}

let p1: Point = { x: 10, y:20 };
p1.x = 5; //error!

let a: number[] = [1, 2, 3, 4];
let ro: ReadonlyArray<number> = a; //once assigned, you can not change
ro[0] = 12; //error
a = ro as number[];

readonly VS const
Variables use const whereas properties use readonly.

Excess Property Checks
let mySqure = createSquare( { width: 100, opacity: 0.5 } as SquareConfig );
This will require the format in SquareConfig

interface SquareConfig {
color?: string;
width?: number;
[propName: string]: any;
}

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

Define the params and return value for Function

let mySearch: SearchFunc;
mySearch = function(source: string, subString: string){
let result = source.search(subString);
return result > -1;
}

Indexable Types
interface StringArray {
[index: number]: string;
}

let myArray: StringArray;
myArray = [“Bob”, “Fred"];
let myStr: string = myArray[0];

Readonly in Indexable Types
interface ReadonlyStringArray{
readonly [index: number]: string;
}
let myArray: ReadonlyStringArray = [“Alice”, “Bob”];
myArray[2] = “Mallory”;

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

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

Extending Interfaces
one interface can extend multiple interfaces.

Hybrid Types
It works, but a little complex to understand. One object can be function and object.

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;

Interfaces Extending Classes
interface will inherits the members of the class, not the implementation


References:
http://sillycat.iteye.com/blog/2285319
http://sillycat.iteye.com/blog/2285543
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值