成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

使用webpack + react + redux + typescript搭建腳手架

imccl / 2422人閱讀

摘要:編譯器會(huì)根據(jù)這個(gè)列表去處理函數(shù)的調(diào)用。泛型泛型接口使用尖括號(hào)傳入泛型變量,它會(huì)根據(jù)參會(huì)類型智能賦值。泛型類在這里,泛型作為一種變量,可供用戶調(diào)用的時(shí)候動(dòng)態(tài)約束類屬性。在泛型約束中使用類型參數(shù)

demo

使用ts完成一個(gè)todomvc的demo

核心概念 接口 參數(shù)校驗(yàn)
 interface LabelledValue {
  label: string;
}

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

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

接口可以用來校驗(yàn)參數(shù)的類型,參數(shù)是否存在等等。

屬性預(yù)定義
interface SquareConfig {
    color?: string;
    width?: number;
  }
  
function createSquare(config: SquareConfig): { color: string; area: number } {
    let newSquare = {color: "white", area: 100};
    if (config.clor) {
      // Error: Property "clor" does not exist on type "SquareConfig"
      newSquare.color = config.clor;
    }
    if (config.width) {
      newSquare.area = config.width * config.width;
    }
    return newSquare;
}
  
let mySquare = createSquare({color: "black"}); 

屬性只讀
interface Point {
    readonly x: number;
    readonly y: number;
} 

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

注意事項(xiàng)
對(duì)象字面量形式會(huì)被特殊對(duì)待而且會(huì)經(jīng)過額外屬性檢查
interface SquareConfig {
    color?: string;
    width?: number;
}

function createSquare(config: SquareConfig): { color: string; area: number } {
    let newSquare = { color: "white", area: 100 };
    if (config.clor) {
        // Error: Property "clor" does not exist on type "SquareConfig"
        newSquare.color = config.clor;
    }
    if (config.width) {
        newSquare.area = config.width * config.width;
    }
    return newSquare;
}

let mySquare = createSquare({ color: "black" });

createSquare({ colour: "red", width: 100 }); 
// Error: Property "colour does no exist on type "SquareConfig"

如果想解決這個(gè)問題有三種解決辦法

// 1. 將對(duì)象字面量復(fù)制給變量,傳入變量不會(huì)被進(jìn)行額外屬性檢查
let skipLiteral = { colour: "red", width: 100 };
createSquare(skipLiteral);

// 2.采用類型斷言,使用as操作符,告訴編譯器我傳入的參數(shù)對(duì)象就是你要的類型
createSquare({ colour: "red", width: 100 } as SquareConfig);

// 3.第三種方式,添加萬能校驗(yàn)符,這個(gè)符號(hào)會(huì)包容color、width之外所有其他屬性,慎用
interface SquareConfig {
    color?: string;
    width?: number;
    [propName: string]: any;
} 
函數(shù)類型
interface SearchFunc {
  (source: string, subString: string): boolean;
} 

可索引的類型
interface StringArray {
  [index: number]: string;
}

let myArray: StringArray;
myArray = ["Bob", "Fred"];

let myStr: string = myArray[0]; 

ts支持兩種索引簽名,字符串和數(shù)值,同時(shí)使用的時(shí)候,數(shù)值索引返回的類型必須是字符串索引返回類型的子類,因?yàn)閖s解析的時(shí)候會(huì)將數(shù)值索引轉(zhuǎn)換為字符串索引,所以數(shù)值索引返回的類型必須與字符串返回的類型保持一致(或子類)。

class Animal {
    name: string;
}
class Dog extends Animal {
    breed: string;
}

// 錯(cuò)誤:使用數(shù)值型的字符串索引,有時(shí)會(huì)得到完全不同的Animal!
interface NotOkay {
    [x: number]: Animal;
    [x: string]: Dog;
} 
類類型

與C#或Java里接口的基本作用一樣,TypeScript也能夠用它來明確的強(qiáng)制一個(gè)類去符合某種契約。

interface ClockInterface {
    currentTime: Date;
}

class Clock implements ClockInterface {
    currentTime: Date;
    constructor(h: number, m: number) { }
} 
接口繼承
interface Shape {
    color: string;
}

interface Square extends Shape {
    sideLength: number;
} 
繼承 extends
class Animal {
    move(distanceInMeters: number = 0) {
        console.log(`Animal moved ${distanceInMeters}m.`);
    }
}

class Dog extends Animal {
    bark() {
        console.log("Woof! Woof!");
    }
}

const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark(); 
存取器
let 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 {
            console.log("Error: Unauthorized update of employee!");
        }
    }
}

let employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
    alert(employee.fullName);
} 
靜態(tài)屬性 static
class Grid {
    static origin = {x: 0, y: 0};
    calculateDistanceFromOrigin(point: {x: number; y: number;}) {
        let xDist = (point.x - Grid.origin.x);
        let yDist = (point.y - Grid.origin.y);
        return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
    }
    constructor (public scale: number) { }
}

let grid1 = new Grid(1.0);  // 1x scale
let grid2 = new Grid(5.0);  // 5x scale

console.log(grid1.calculateDistanceFromOrigin({x: 10, y: 10}));
console.log(grid2.calculateDistanceFromOrigin({x: 10, y: 10})); 
把類當(dāng)做接口使用
class Point {
    x: number;
    y: number;
}

interface Point3d extends Point {
    z: number;
}

let point3d: Point3d = {x: 1, y: 2, z: 3}; 
函數(shù)
let myAdd: (x: number, y: number) => number =
    function(x: number, y: number): number { return x + y; }; 
重載

方法是為同一個(gè)函數(shù)提供多個(gè)函數(shù)類型定義來進(jìn)行函數(shù)重載。 編譯器會(huì)根據(jù)這個(gè)列表去處理函數(shù)的調(diào)用。

let suits = ["hearts", "spades", "clubs", "diamonds"];

function pickCard(x: {suit: string; card: number; }[]): number;
function pickCard(x: number): {suit: string; card: number; };
function pickCard(x): any {
    // Check to see if we"re working with an object/array
    // if so, they gave us the deck and we"ll pick the card
    if (typeof x == "object") {
        let pickedCard = Math.floor(Math.random() * x.length);
        return pickedCard;
    }
    // Otherwise just let them pick the card
    else if (typeof x == "number") {
        let pickedSuit = Math.floor(x / 13);
        return { suit: suits[pickedSuit], card: x % 13 };
    }
}

let myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }];
let pickedCard1 = myDeck[pickCard(myDeck)];
alert("card: " + pickedCard1.card + " of " + pickedCard1.suit);

let pickedCard2 = pickCard(15);
alert("card: " + pickedCard2.card + " of " + pickedCard2.suit); 
泛型 泛型接口
function identity(arg: T): T {
    return arg;
} 

使用尖括號(hào)傳入泛型變量,它會(huì)根據(jù)參會(huì)類型智能賦值。比如arg是number,那么T就會(huì)被賦值為number。

泛型類
class GenericNumber {
    zeroValue: T;
    add: (x: T, y: T) => T;
}

let myGenericNumber = new GenericNumber();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) { return x + y; }; 

在這里,泛型作為一種變量,可供用戶調(diào)用的時(shí)候動(dòng)態(tài)約束類屬性。

在泛型約束中使用類型參數(shù)
function getProperty(obj: T, key: K) {
    return obj[key];
}

let x = { a: 1, b: 2, c: 3, d: 4 };

getProperty(x, "a"); // okay
getProperty(x, "m"); // error: Argument of type "m" isn"t assignable to "a" | "b" | "c" | "d". 

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/99966.html

相關(guān)文章

  • React的移動(dòng)端和PC端生態(tài)圈的使用匯總

    摘要:調(diào)用通過注冊(cè)表調(diào)用到實(shí)例,透過的,調(diào)用到中的,最后通過,調(diào)用,根據(jù)參數(shù)相應(yīng)模塊執(zhí)行。京東的,多端解決方案是一套遵循語法規(guī)范的多端開發(fā)解決方案。 showImg(https://segmentfault.com/img/bVbuMkw?w=1304&h=808); 對(duì)于一項(xiàng)技術(shù),我們不能停留在五分鐘狀態(tài),特別喜歡一句話,用什么方式繪制UI界面一點(diǎn)不重要,重要的是底層的思維,解決問題和優(yōu)化...

    kun_jian 評(píng)論0 收藏0
  • React的移動(dòng)端和PC端生態(tài)圈的使用匯總

    摘要:調(diào)用通過注冊(cè)表調(diào)用到實(shí)例,透過的,調(diào)用到中的,最后通過,調(diào)用,根據(jù)參數(shù)相應(yīng)模塊執(zhí)行。京東的,多端解決方案是一套遵循語法規(guī)范的多端開發(fā)解決方案。 showImg(https://segmentfault.com/img/bVbuMkw?w=1304&h=808); 對(duì)于一項(xiàng)技術(shù),我們不能停留在五分鐘狀態(tài),特別喜歡一句話,用什么方式繪制UI界面一點(diǎn)不重要,重要的是底層的思維,解決問題和優(yōu)化...

    J4ck_Chan 評(píng)論0 收藏0
  • React的移動(dòng)端和PC端生態(tài)圈的使用匯總

    摘要:調(diào)用通過注冊(cè)表調(diào)用到實(shí)例,透過的,調(diào)用到中的,最后通過,調(diào)用,根據(jù)參數(shù)相應(yīng)模塊執(zhí)行。京東的,多端解決方案是一套遵循語法規(guī)范的多端開發(fā)解決方案。 showImg(https://segmentfault.com/img/bVbuMkw?w=1304&h=808); 對(duì)于一項(xiàng)技術(shù),我們不能停留在五分鐘狀態(tài),特別喜歡一句話,用什么方式繪制UI界面一點(diǎn)不重要,重要的是底層的思維,解決問題和優(yōu)化...

    Travis 評(píng)論0 收藏0
  • TypeScript 、ReactRedux和Ant-Design的最佳實(shí)踐

    摘要:使用官方的的另外一種版本和一起使用自動(dòng)配置了一個(gè)項(xiàng)目支持。需要的依賴都在文件中。帶靜態(tài)類型檢驗(yàn),現(xiàn)在的第三方包基本上源碼都是,方便查看調(diào)試。大型項(xiàng)目首選和結(jié)合,代碼調(diào)試維護(hù)起來極其方便。 showImg(https://segmentfault.com/img/bVbrTKz?w=1400&h=930); 阿特伍德定律,指的是any application that can be wr...

    wangbinke 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<