Typescript 声明文件是一种特殊的文件,它可以帮助开发者在使用 JavaScript 进行开发时提供类型检查和代码补全功能。它是一个 JavaScript 的超集,它支持 JavaScript 的所有语法,并且还增加了一些额外的特性,如类型注释、接口、命名空间、模块等。
Typescript 声明文件的核心是定义一个全局对象(或者说是一个函数)来声明你想要使用的库或者 API 的名字、函数、参数、返回值等信息。声明文件通常使用 .d.ts 文件扩展名来保存。
declare function myFunc(param1: string, param2: number): boolean;
上面的代码声明了一个名为 myFunc 的函数,它有两个参数 param1 和 param2 ,分别是字符串和数字类型,并返回布尔值。当你在使用 JavaScript 时遇到不能正常工作的情况时,Typescript 声明文件就会起作用。它会告诉你出错的原因(如参数不匹配或者返回值不正确),并提供相应的解决方法。
// 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 { };
// 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;
}
}
// 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);
}
// 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;
}
}
// 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;
}
// 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;
}
}
// 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;
}
1. Iterator(遍历器)的概念JavaScript 原有的表示“集合”的数据结构,主要是数组( Array )和对象( Object ),ES6 又添加...
ECMAScript 6 教程导读ES6:全称ECMAScript 6.0ECMAScript 是JavaScript语言的国际标准,JavaScript是ECMAScript的实现。ES6经过...
HTML DOM 简介 HTML DOM 定义了访问和操作 HTML 文档的标准。您应该具备的基础知识 在您开始学习HTMLDOM之前,您需要对以下内容...
描述导入指令,导入SASS或SCSS文件。 它直接需要导入文件名。 在SASS中导入的所有文件将在单个CSS文件中组合。当我们使用 @impor...
描述 @for 指令允许您在循环中生成样式。计数器变量用于设置每次迭代的输出。 @for 指令有两种类型,如下表所示:序号关键字和描...
描述本节介绍了Less 中函数的使用。LESS 映射具有值操作的 JavaScript 代码,并使用预定义的函数来操纵样式表中的 HTML 元素。它...
实例使用 meter 元素展示给定的数据范围:meter value="2" min="0" max="10"2 out of 10/meterbrmeter value="0.6"60%/meter浏览...
RowSetInternalpublic interface RowSetInternalRowSet 对象为了将自身呈现给 RowSetReader 或 RowSetWriter 对象而实现的接口。...