<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                >[success] # extends 泛型約束 ~~~ 1.泛型現在似乎可以是任何類型,但實際開發可能往往不是任意類型,需要給以一個范圍,這種就叫'泛型約束'關鍵字('extends') 泛型是具有當前指定的屬性寫法上'<T extends xx>' 2.注意泛型約束是約束泛型的 在<> 這里寫 ~~~ >[danger] ##### 泛型約束 ~~~ 1.限定了泛型入參只能是 number | string | boolean 的子集 ~~~ ~~~ function reflectSpecified<P extends number | string | boolean>(param: P):P { return param; } reflectSpecified('string'); // ok reflectSpecified(1); // ok reflectSpecified(true); // ok reflectSpecified(null); // ts(2345) 'null' 不能賦予類型 'number | string | boolean' ~~~ ~~~ interface ReduxModelSpecified<State extends { id: number; name: string }> { state: State } type ComputedReduxModel1 = ReduxModelSpecified<{ id: number; name: string; }>; // ok type ComputedReduxModel2 = ReduxModelSpecified<{ id: number; name: string; age: number; }>; // ok type ComputedReduxModel3 = ReduxModelSpecified<{ id: string; name: number; }>; // ts(2344) type ComputedReduxModel4 = ReduxModelSpecified<{ id: number;}>; // ts(2344) ~~~ >[danger] ##### 泛型約束結合索引類型的使用 ~~~ 1.看下面案例想 獲取對象value 輸出出來 type Info = { name:string age:number } function getVal(obj:Info, key:any) { return obj[key] // 報錯 } ~~~ ![](https://img.kancloud.cn/25/99/2599f2ee01c774e5002910a712b04141_954x235.png) * 正確寫法可以利用keyof 吧傳入的對象的屬性類型取出生成一個聯合類型 ~~~ type Info = { name:string age:number } function getVal(obj:Info, key:keyof Info) { return obj[key] } ~~~ * 使用泛型 ~~~ 1.利用'索引類型 keyof T 把傳入的對象的屬性類型取出生成一個聯合類型',再用'extends 做約束' ~~~ ~~~ // 注意泛型約束是約束泛型的 在<> 這里寫 type GetVal = <T extends object, K extends keyof T>(obj: T, key: K) => string function getVal(obj: any, key: any): GetVal { return obj[key] } getVal({ name: 'w' }, 'name') ~~~ >[danger] ##### 多重約束 ~~~ interface FirstInterface { doSomething(): number } interface SecondInterface { doSomethingElse(): string } // // interface ChildInterface extends FirstInterface, SecondInterface {} 二者等同 class Demo<T extends FirstInterface & SecondInterface> { private genericProperty: T useT() { this.genericProperty.doSomething() // ok this.genericProperty.doSomethingElse() // ok } } ~~~
                  <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>

                              哎呀哎呀视频在线观看