美文网首页1024Learning TypescriptTypeScript基础
10、Typescript泛型之泛型接口

10、Typescript泛型之泛型接口

作者: 圆梦人生 | 来源:发表于2019-03-07 19:41 被阅读7次

    案例

    interface InterfaceFn {
        //
        <T>(string:T):T
    }
    
    let getStr:InterfaceFn = <T>(str:T):T =>{
        //
        return str;
    }
    console.log(getStr('fxgetStr'));
    console.log(getStr(123456));
    // 泛型接口2
    interface InterfaceFn2<T> {
        (string:T):T
    }
    //
    let getData = function<T>(str:T):T{
        //
        return str;
    }
    let getstr2:InterfaceFn2<string> = getData;
    let getstr3:InterfaceFn2<number> = getData;
    console.log(getstr2('1233-字符串'));
    console.log(getstr3(1233));
    
    

    相关文章

      网友评论

        本文标题:10、Typescript泛型之泛型接口

        本文链接:https://www.haomeiwen.com/subject/nwbhpqtx.html