typescirpt - .d.ts声明文件的用法

1、如果是可调用的方法,需要像下面这样声明:

declare function myLib(a: string): string;
declare function myLib(a: number): number;

调用的时候像这样: 

myLib(3)

2、如果希望此库的名称是有效的类型名称:

interface myLib {
    name: string;
    length: number;
    extras?: string[];
}

你调用的时候会这样:

var x: myLib

3、如果您的库具有在全局变量上公开的属性:

declare namespace myLib {
    //~ We can write 'myLib.timeout = 50;'
    let timeout: number;

    //~ We can access 'myLib.version', but not change it
    const version: string;

    //~ There's some class we can create via 'let c = new myLib.Cat(42)'
    //~ Or reference e.g. 'function f(c: myLib.Cat) { ... }
    class Cat {
        constructor(n: number);

        //~ We can read 'c.age' from a 'Cat' instance
        readonly age: number;

        //~ We can invoke 'c.purr()' from a 'Cat' instance
        purr(): void;
    }

    //~ We can declare a variable as
    //~   'var s: myLib.CatSettings = { weight: 5, name: "Maru" };'
    interface CatSettings {
        weight: number;
        name: string;
        tailLength?: number;
    }

    //~ We can write 'const v: myLib.VetID = 42;'
    //~  or 'const v: myLib.VetID = "bob";'
    type VetID = string | number;

    //~ We can invoke 'myLib.checkCat(c)' or 'myLib.checkCat(c, v);'
    function checkCat(c: Cat, s?: VetID);
}

 

转载于:https://www.cnblogs.com/hellolol/p/11059465.html

你可能感兴趣的:(typescirpt - .d.ts声明文件的用法)