`
sillycat
  • 浏览: 2551031 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

    博客分类:
  • UI
 
阅读更多
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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics