<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 寬松版參數檢查 非接口 ``` function printLabel(labelledObj: { label: string }) { console.log(labelledObj.label); } let myObj = { size: 10, label: "Size 10 Object" }; printLabel(myObj); ``` 接口 ``` interface LabelledObj{ label:string } function printLabel(labelledObj: LabelledObj) { console.log(labelledObj.label); } let myObj = { size: 10, label: "Size 10 Object" }; printLabel(myObj); ``` ## 可選屬性 ``` interface SquareConfig { color?: string; width?: number; } ``` ## 只讀屬性 `readonly`來指定只讀屬性: ``` interface Point { readonly x: number; readonly y: number; } ``` ``` let p1: Point = { x: 10, y: 20 }; p1.x = 5; // error! ``` 數組只讀 `ReadonlyArray<T>` ``` let a: number[] = [1, 2, 3, 4]; let ro: ReadonlyArray<number> = a; ro[0] = 12; // error! ro.push(5); // error! ro.length = 100; // error! a = ro; // error! ``` 普通復制不被允許,只能通過斷言賦值 `a = ro as number[]; ` ## 定義函數類型 ``` interface SearchFunc { (source: string, subString: string): boolean; } ``` 參數名可不一致 ``` let mySearch: SearchFunc; mySearch = function(source: string, subString: string) { let result = source.search(subString); return result > -1; } ``` 有可在函數中取消類型聲明 ``` let mySearch: SearchFunc; mySearch = function(src, sub) { let result = src.search(sub); return result > -1; } ``` ## 可索引的類型 如`a[10]`或`ageMap["daniel"]` ``` interface Foo{ [index:number]:string } let F:Foo F=["a","b","c"] F[0]==F['0'] //"a" ``` 查詢索引時,`number`與`string`相同 設置只讀索引 ``` interface ReadonlyStringArray { readonly [index: number]: string; } let myArray: ReadonlyStringArray = ["Alice", "Bob"]; myArray[2] = "Mallory"; // error! ``` ## 類接口 定義屬性和方法 ``` interface ClockInterface { currentTime: Date; setTime(d: Date); } class Clock implements ClockInterface { currentTime: Date; setTime(d: Date) { this.currentTime = d; } getTime():string{ //可添加私有方法 return new Date().toLocaleTimeString() } constructor(h: number, m: number) { } } ``` ## 繼承接口 ``` interface Foo{ Name:string } interface Fooo extends Foo{ Age:number } let f = <Fooo>{Name:"cc",Age:12} f.Name="ddd" console.log(f.Name); ``` 可繼承多個接口 ``` interface Shape { color: string; } interface PenStroke { penWidth: number; } interface Square extends Shape, PenStroke { sideLength: number; } ``` ## 混合類型 ``` 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; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看