From de04f34240dd46c3cfde395f3818d663a569ed5d Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Wed, 25 Sep 2019 02:00:05 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Add=C2=A0`webidl=E2=80=91conversions`?= =?UTF-8?q?=20(#38450)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Add `webidl‑conversions` * fix(webidl‑conversions): Fix on TypeScript 3.0 * refactor: Apply Prettier style --- types/webidl-conversions/.editorconfig | 3 + types/webidl-conversions/index.d.ts | 86 +++++++++++++++++++ types/webidl-conversions/tsconfig.json | 23 +++++ types/webidl-conversions/tslint.json | 1 + .../webidl-conversions-tests.ts | 70 +++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 types/webidl-conversions/.editorconfig create mode 100644 types/webidl-conversions/index.d.ts create mode 100644 types/webidl-conversions/tsconfig.json create mode 100644 types/webidl-conversions/tslint.json create mode 100644 types/webidl-conversions/webidl-conversions-tests.ts diff --git a/types/webidl-conversions/.editorconfig b/types/webidl-conversions/.editorconfig new file mode 100644 index 0000000000..a0207fa43b --- /dev/null +++ b/types/webidl-conversions/.editorconfig @@ -0,0 +1,3 @@ +# This package uses tabs +[*] +indent_style = tab diff --git a/types/webidl-conversions/index.d.ts b/types/webidl-conversions/index.d.ts new file mode 100644 index 0000000000..fedd3595ef --- /dev/null +++ b/types/webidl-conversions/index.d.ts @@ -0,0 +1,86 @@ +// Type definitions for webidl-conversions 4.0 +// Project: https://github.com/jsdom/webidl-conversions#readme +// Definitions by: ExE Boss +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +type Parameters any> = T extends (...args: infer P) => any ? P : never; + +declare namespace WebIDLConversions { + interface Options { + context?: string; + } + + interface IntegerOptions extends Options { + enforceRange?: boolean; + clamp?: boolean; + } + + interface StringOptions extends Options { + treatNullAsEmptyString?: boolean; + } + + type IntegerConversion = (V: any, opts?: IntegerOptions) => number; + type StringConversion = (V: any, opts?: StringOptions) => string; + type NumberConversion = (V: any, opts?: Options) => number; +} + +declare const WebIDLConversions: { + any(V: V, opts?: WebIDLConversions.Options): V; + void(V?: any, opts?: WebIDLConversions.Options): void; + boolean(V: any, opts?: WebIDLConversions.Options): boolean; + + byte(V: any, opts?: WebIDLConversions.IntegerOptions): number; + octet(V: any, opts?: WebIDLConversions.IntegerOptions): number; + + short(V: any, opts?: WebIDLConversions.IntegerOptions): number; + ['unsigned short'](V: any, opts?: WebIDLConversions.IntegerOptions): number; + + long(V: any, opts?: WebIDLConversions.IntegerOptions): number; + ['unsigned long'](V: any, opts?: WebIDLConversions.IntegerOptions): number; + + ['long long'](V: any, opts?: WebIDLConversions.IntegerOptions): number; + ['unsigned long long'](V: any, opts?: WebIDLConversions.IntegerOptions): number; + + double(V: any, opts?: WebIDLConversions.Options): number; + ['unrestricted double'](V: any, opts?: WebIDLConversions.Options): number; + + float(V: any, opts?: WebIDLConversions.Options): number; + ['unrestricted float'](V: any, opts?: WebIDLConversions.Options): number; + + DOMString(V: any, opts?: WebIDLConversions.StringOptions): string; + ByteString(V: any, opts?: WebIDLConversions.StringOptions): string; + USVString(V: any, opts?: WebIDLConversions.StringOptions): string; + + object(V: any, opts?: WebIDLConversions.Options): object; + Error(V: any, opts?: WebIDLConversions.Options): Error; + + ArrayBuffer(V: any, opts?: WebIDLConversions.Options): ArrayBuffer; + DataView(V: any, opts?: WebIDLConversions.Options): DataView; + + Int8Array(V: any, opts?: WebIDLConversions.Options): Int8Array; + Int16Array(V: any, opts?: WebIDLConversions.Options): Int16Array; + Int32Array(V: any, opts?: WebIDLConversions.Options): Int32Array; + + Uint8Array(V: any, opts?: WebIDLConversions.Options): Uint8Array; + Uint16Array(V: any, opts?: WebIDLConversions.Options): Uint16Array; + Uint32Array(V: any, opts?: WebIDLConversions.Options): Uint32Array; + Uint8ClampedArray(V: any, opts?: WebIDLConversions.Options): Uint8ClampedArray; + + Float32Array(V: any, opts?: WebIDLConversions.Options): Float32Array; + Float64Array(V: any, opts?: WebIDLConversions.Options): Float64Array; + + ArrayBufferView(V: any, opts?: WebIDLConversions.Options): ArrayBufferView; + BufferSource(V: any, opts?: WebIDLConversions.Options): ArrayBuffer | ArrayBufferView; + + DOMTimeStamp(V: any, opts?: WebIDLConversions.Options): number; + // tslint:disable:ban-types + Function(V: V, opts?: WebIDLConversions.Options): V extends ((...args: any[]) => any) ? V : Function; + VoidFunction( + V: V, + opts?: WebIDLConversions.Options, + ): V extends ((...args: any[]) => any) ? (...args: Parameters) => void : Function; +}; + +// This can't use ES6 style exports, as those can't have spaces in export names. +export = WebIDLConversions; diff --git a/types/webidl-conversions/tsconfig.json b/types/webidl-conversions/tsconfig.json new file mode 100644 index 0000000000..efe968e66b --- /dev/null +++ b/types/webidl-conversions/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es2015" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "webidl-conversions-tests.ts" + ] +} diff --git a/types/webidl-conversions/tslint.json b/types/webidl-conversions/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/webidl-conversions/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/webidl-conversions/webidl-conversions-tests.ts b/types/webidl-conversions/webidl-conversions-tests.ts new file mode 100644 index 0000000000..3ae2767a93 --- /dev/null +++ b/types/webidl-conversions/webidl-conversions-tests.ts @@ -0,0 +1,70 @@ +import conversions = require('webidl-conversions'); + +const any: any = void 0; +const unknown: unknown = void 0; +// $ExpectType Options +const options: conversions.Options = ((): conversions.Options => { + return {}; +})(); + +conversions.any(any); // $ExpectType any +conversions.any(unknown); // $ExpectType unknown +conversions.any(options); // $ExpectType Options +conversions.any(RegExp); // $ExpectType RegExpConstructor +conversions.any(Symbol.toStringTag); // $ExpectType symbol + +conversions.void(); // $ExpectType void +conversions.boolean(any); // $ExpectType boolean + +conversions.byte(any); // $ExpectType number +conversions.octet(any); // $ExpectType number + +conversions.short(any); // $ExpectType number +conversions['unsigned short'](any); // $ExpectType number + +conversions.long(any); // $ExpectType number +conversions['unsigned long'](any); // $ExpectType number + +conversions['long long'](any); // $ExpectType number +conversions['unsigned long long'](any); // $ExpectType number + +conversions.double(any); // $ExpectType number +conversions['unrestricted double'](any); // $ExpectType number + +conversions.float(any); // $ExpectType number +conversions['unrestricted float'](any); // $ExpectType number + +conversions.DOMString(any, options); // $ExpectType string +conversions.ByteString(any, options); // $ExpectType string +conversions.USVString(any, options); // $ExpectType string + +conversions.object(any, options); // $ExpectType object +conversions.Error(any, options); // $ExpectType Error + +conversions.ArrayBuffer(any, options); // $ExpectType ArrayBuffer +conversions.DataView(any, options); // $ExpectType DataView + +conversions.Int8Array(any, options); // $ExpectType Int8Array +conversions.Int16Array(any, options); // $ExpectType Int16Array +conversions.Int32Array(any, options); // $ExpectType Int32Array + +conversions.Uint8Array(any, options); // $ExpectType Uint8Array +conversions.Uint16Array(any, options); // $ExpectType Uint16Array +conversions.Uint32Array(any, options); // $ExpectType Uint32Array +conversions.Uint8ClampedArray(any, options); // $ExpectType Uint8ClampedArray + +conversions.Float32Array(any, options); // $ExpectType Float32Array +conversions.Float64Array(any, options); // $ExpectType Float64Array + +conversions.ArrayBufferView(any, options); // $ExpectType ArrayBufferView +conversions.BufferSource(any, options); // $ExpectType ArrayBuffer | ArrayBufferView + +conversions.DOMTimeStamp(any, options); // $ExpectType number + +conversions.Function(any, options); // $ExpectType any +conversions.Function(unknown, options); // $ExpectType Function +conversions.Function((arg: any) => true, options); // $ExpectType (arg: any) => true + +conversions.VoidFunction(any, options); // $ExpectType Function | ((...args: unknown[]) => void) +conversions.VoidFunction(unknown, options); // $ExpectType Function +conversions.VoidFunction((arg: any) => true, options); // $ExpectType (arg: any) => void