ES5 APIS 声明文件解析
声明文件定义
declare var NaN: number
interface Symbol {
toString(): string;
valueOf(): symbol;
}
interface Number {
toString(radix?: number): string;
toFixed(fractionDigits?: number): string;
toExponential(fractionDigits?: number): string;
toPrecision(precision?: number): string;
valueOf(): boolean;
}
interface NumberConstructor {
new(value?: any): Number;
(value?: any): number;
readonly prototype: Number;
readonly MAX_VALUE: number;
readonly MIN_VALUE: number;
readonly NaN: number;
readonly NEGATIVE_INFINITY: number;
readonly POSITIVE_INFINITY: number;
}
declare var Number: NumberConstructor;
interface String {
toString(): any;
charAt(pos: number): string;
charCodeAt(index: number): string;
concat(...string: string[]): string;
indexOf(searchString: string, position?: number): number;
lastIndexOf(searchString: string, position?: number): number;
localeCompare(that: string): number;
match(regexp: string | RegExp): RegExpMatchArray | null;
replace(searchValue: string | RegExp, replaceValue: string): string;
replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
search(regexp: string | RegExp): number;
slice(start?: number, end?: number): string;
split(separator: string | RegExp, limit?: number): string[];
substring(start: number, end?: number): string;
toLowerCase(): string;
toLocaleLowerCase(): string;
toUpperCase(): string;
toLocaleUpperCase(): string;
trim(): string;
readonly length: number;
substr(form: number, length?: number): string;
valueOf(): string;
readonly [index: number]: string
}
interface StringConstructor {
new(value?: any): String;
(value?: any): string;
readonly prototype: String;
fromCharCode(...codes: number[]): string
}
declare var String: StringConstructor;
interface Boolean {
valueOf(): boolean;
}
interface BooleanConstructor {
new(value?: any): Boolean;
<T>(value?: T): boolean;
readonly prototype: Boolean;
}
declare var Boolean: BooleanConstructor;
interface PropertyDescriptor {
configurable?: boolean;
enumerable?: boolean;
value?: any;
writable?: boolean;
get?(): any;
set?(val: any): void;
}
interface PropertyDescriptorMap {
[s: string]: PropertyDescriptor
}
interface Object {
constructor: Function;
toString(): string;
toLocalString(): string;
valueOf(): Object;
hasOwnProperty(v: PropertyKey): boolean;
isPrototypeOf(v: Object): boolean;
propertyIsEnumerable(v: PropertyKey): boolean;
}
interface ObjectConstructor {
new(value?: any): Object;
(): any;
(value: any): any;
readonly prototype: Object;
getPrototypeOf(o: any): any;
getOwnPropertyDescriptor(o: any, p: PropertyKey): PropertyDescriptor | undefined;
getOwnPropertyNames(o: any): string[];
create(o: Object | null): any;
create(o: object | null, properties: PropertyDescriptorMap & ThisType<any>): any;
defineProperty<T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>): T;
defineProperties<T>(o: T, properties: PropertyDescriptorMap & ThisType<any>): T;
seal<T>(o: T): T;
freeze<T>(a: T[]): readonly T[];
freeze<T extends Function>(f: T): T;
freeze<T>(o: T): Readonly<T>;
preventExtensions<T>(o: T): T;
isSealed(o: any): boolean;
isFrozen(o: any): boolean;
isExtensible(o: any): boolean;
keys(o: object): string[];
}
declare var Object: ObjectConstructor;
interface ConcatArray<T> {
readonly length: number;
readonly [n: number]: T;
join(separator?: string): string;
slice(start?: number, end?: number): T[];
}
interface Array<T> {
length: number;
toString(): string;
toLocaleString(): string;
pop(): T | undefined;
push(...items: T[]): number;
concat(...items: ConcatArray<T>[]): T[];
concat(...items: (T | ConcatArray<T>)[]): T[]
join(separator?: string): string;
reverse(): T | undefined;
shift(): T | undefined;
slice(start?: number, end?: number): T[];
sort(compareFn?: (a: T, b: T) => number): this;
splice(start: number, deleteCount: number, ...items: T[]): T[];
unshift(...items: T[]): number;
indexOf(searchElement: T, fromIndex?: number): number;
lastIndexOf(searchElement: T, fromIndex?: number): number;
every<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[];
every(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
forEach(callbackFn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
map<U>(callbackFn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
filter<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];
reduce(callbackFn: (perviousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
reduce(callbackFn: (perviousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
reduce<U>(callbackFn: (perviousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
reduceRight(callbackFn: (perviousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
reduceRight(callbackFn: (perviousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
reduceRight<U>(callbackFn: (perviousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
[n: number]: T;
}
interface ArrayConstructor {
new(arrayLength?: number): any[];
new <T>(arrayLength?: number): T[];
new <T>(...items: T[]): T[];
(arrayLength?: number): any[];
<T>(arrayLength?: number): T[];
<T>(...items: T[]): T[];
isArray(arg: any): arg is any[];
readonly prototype: any[];
}
declare var Array: ArrayConstructor;
interface Function {
apply(this: Function, thisArg: any, argArray?: any): any;
call(this: Function, thisArg: any, ...argArray: any[]): any;
bind(this: Function, thisArg: any, ...argArray: any[]): any;
toString(): any;
prototype: any;
readonly length: number;
arguments: any;
caller: Function
}
interface FunctionConstructor {
new(...args: string[]): Function;
(...args: string[]): Function;
readonly prototype: Function;
}
declare var Function: FunctionConstructor;
interface PromiseLike<T> {
then<TResult1 = T, TResult2 = never>(
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
): PromiseLike<TResult1 | TResult2>;
}
interface Promise<T> {
then<TResult1 = T, TResult2 = never>(
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
): Promise<TResult1 | TResult2>;
catch<TResult = never>(
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null
): Promise<T | TResult>
}
type Partial<T> = {
[P in keyof T]?: T[P]
}
type Readonly<T> = {
readonly [P in keyof T]: T[P]
}
type Pick<T, K extends keyof T> = {
[P in K]: T[P]
}
type Required<T> = {
[P in keyof T]-?: T[P]
}
type Record<K extends keyof any, T> = {
[P in K]: T
}
type Exclude<T, U> = T extends U ? never : T
type Extract<T, U> = T extends U ? T : never
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>
type NonNullable<T> = T extends null | undefined ? never : T
type Parameters<T extends (...args: any) => any>
= T extends (...args: infer P) => any ? P : never
type ConstructorParmeters<T extends abstract new (...args: any) => any>
= T extends abstract new (...args: infer P) => any ? P : never
type ReturnType<T extends (...args: any) => any>
= T extends (...args: any) => infer R ? R : any
type InstanceType<T extends abstract new (...args: any) => any>
= T extends abstract new (...args: any) => infer R ? R : any
type UpperCase<S extends string> = intrinsic
type Lowercase<S extends string> = intrinsic
type Capitalize<S extends string> = intrinsic
type Uncapitalize<S extends string> = intrinsic
typescript
工具类型定义测试
interface Person {
name: string;
age: number,
sex: 'male' | 'wolf'
}
type Person2 = Required<Person>
const person: Person2 = {
name: '123',
age: 23,
sex: 'male'
}
type Person3 = Pick<Person, 'name' | 'sex'>
type Person4 = Record<'sex' | 'male', string>
type Person5 = Record<'heora' | 'yuleuo', Person>
type Person6 = Exclude<Person3, Person>
type Person7 = Exclude<keyof Person, keyof Person3>
type Test1 = Exclude<'a' | 'b' | 'c', 'a' | 'b'>
type Test2 = Extract<'a' | 'b' | 'c', 'a' | 'b'>
type Person8 = Omit<Person, 'name' | 'age'>
type Test3 = NonNullable<string | number | null | undefined>
function parametersFunc(a: string, b: number, c: boolean) {
return a || b || c;
}
type Test4 = Parameters<typeof parametersFunc>
class Dog {
constructor(public name: string, public age: number) {
this.name = name
this.age = age
}
}
type Test5 = ConstructorParameters<typeof Dog>
type Test6 = ReturnType<typeof parametersFunc>
type Test7 = InstanceType<typeof Dog>
type Test8 = InstanceType<typeof Boolean>
type Greeting = "Hello, world"
type ShoutyGreeting = Uppercase<Greeting>
type QuietGreeting = Lowercase<Greeting>
type LowercaseGreeting = "hello, world";
type Greeting1 = Capitalize<LowercaseGreeting>;
type UppercaseGreeting = "HELLO WORLD";
type UncomfortableGreeting = Uncapitalize<UppercaseGreeting>;
typescript