您的位置:58脚本 > typescript 声明文件 TypeScript 声明文件模板

typescript 声明文件 TypeScript 声明文件模板

2023-03-18 00:32 TypeScript教程

typescript 声明文件 TypeScript 声明文件模板

typescript 声明文件

Typescript 声明文件是一种特殊的文件,它可以帮助开发者在使用 JavaScript 进行开发时提供类型检查和代码补全功能。它是一个 JavaScript 的超集,它支持 JavaScript 的所有语法,并且还增加了一些额外的特性,如类型注释、接口、命名空间、模块等。

Typescript 声明文件的核心是定义一个全局对象(或者说是一个函数)来声明你想要使用的库或者 API 的名字、函数、参数、返回值等信息。声明文件通常使用 .d.ts 文件扩展名来保存。

declare function myFunc(param1: string, param2: number): boolean; 

上面的代码声明了一个名为 myFunc 的函数,它有两个参数 param1 和 param2 ,分别是字符串和数字类型,并返回布尔值。当你在使用 JavaScript 时遇到不能正常工作的情况时,Typescript 声明文件就会起作用。它会告诉你出错的原因(如参数不匹配或者返回值不正确),并提供相应的解决方法。

TypeScript 声明文件模板

  • global-modifying-module.d.ts
  • global-plugin.d.ts
  • global.d.ts
  • module-class.d.ts
  • module-function.d.ts
  • module-plugin.d.ts
  • module.d.ts

global-modifying-module.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>




declare global {
    
    interface String {
        fancyFormat(opts: StringFormatOptions): string;
    }
}


export interface StringFormatOptions {
    fancinessLevel: number;
}


export function doSomething(): void;


export { };

global-plugin.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>




interface Number {
    toBinaryString(opts?: MyLibrary.BinaryFormatOptions): string;
    toBinaryString(callback: MyLibrary.BinaryFormatCallback, opts?: MyLibrary.BinaryFormatOptions): string;
}


declare namespace MyLibrary {
    type BinaryFormatCallback = (n: number) => string;
    interface BinaryFormatOptions {
        prefix?: string;
        padding: number;
    }
}

global.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>


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


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


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);
}

module-class.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>






export as namespace myClassLib;


export = MyClass;


declare class MyClass {
    constructor(someParam?: string);

    someProperty: string[];

    myMethod(opts: MyClass.MyClassMethodOptions): number;
}


declare namespace MyClass {
    export interface MyClassMethodOptions {
        width?: number;
        height?: number;
    }
}

module-function.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>






export as namespace myFuncLib;


export = MyFunction;


declare function MyFunction(name: string): MyFunction.NamedReturnType;
declare function MyFunction(length: number): MyFunction.LengthReturnType;


declare namespace MyFunction {
    export interface LengthReturnType {
        width: number;
        height: number;
    }
    export interface NamedReturnType {
        firstName: string;
        lastName: string;
    }

    
    export const defaultName: string;
    export let defaultLength: number;
}

module-plugin.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>




import * as m from "someModule";


import * as other from "anotherModule";


declare module "someModule" {
    
    export function theNewMethod(x: m.foo): other.bar;

    
    export interface SomeModuleOptions {
        someModuleSetting?: string;
    }

    
    export interface MyModulePluginOptions {
        size: number;
    }
}

module.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~]
// Project: [~THE PROJECT NAME~]
// Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]>




export as namespace myLib;


export function myMethod(a: string): string;
export function myOtherMethod(a: number): number;


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


export const myField: number;


export namespace subProp {
    
    export function foo(): void;
}

阅读全文
以上是58脚本为你收集整理的typescript 声明文件 TypeScript 声明文件模板全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 58脚本 58jiaoben.com 版权所有 联系我们
桂ICP备12005667号-28 Powered by CMS