diff --git a/types/create-html-element/create-html-element-tests.ts b/types/create-html-element/create-html-element-tests.ts new file mode 100644 index 0000000000..e94b6cb617 --- /dev/null +++ b/types/create-html-element/create-html-element-tests.ts @@ -0,0 +1,48 @@ +import createHtmlElement = require('create-html-element'); + +createHtmlElement(); // $ExpectType HTMLDivElement +// $ExpectType HTMLDivElement +createHtmlElement({ + attributes: { + class: 'unicorn', + rainbow: true, + horse: false, + number: 1, + multiple: ['a', 'b'], + }, +}); +createHtmlElement({ html: '🦄' }); // $ExpectType HTMLDivElement +createHtmlElement({ text: 'Hello World' }); // $ExpectType HTMLDivElement +createHtmlElement({ html: '🦄', text: 'Hello World' }); // $ExpectError + +createHtmlElement({ name: 'h1' }); // $ExpectType HTMLHeadingElement +// $ExpectType HTMLHeadingElement +createHtmlElement({ + name: 'h1', + attributes: { + class: 'unicorn', + rainbow: true, + horse: false, + number: 1, + multiple: ['a', 'b'], + }, +}); +createHtmlElement({ name: 'h1', html: '🦄' }); // $ExpectType HTMLHeadingElement +createHtmlElement({ name: 'h1', text: 'Hello World' }); // $ExpectType HTMLHeadingElement +createHtmlElement({ name: 'h1', html: '🦄', text: 'Hello World' }); // $ExpectError + +createHtmlElement({ name: 'foo' }); // $ExpectType HTMLElement +// $ExpectType HTMLElement +createHtmlElement({ + name: 'foo', + attributes: { + class: 'unicorn', + rainbow: true, + horse: false, + number: 1, + multiple: ['a', 'b'], + }, +}); +createHtmlElement({ name: 'foo', html: '🦄' }); // $ExpectType HTMLElement +createHtmlElement({ name: 'foo', text: 'Hello World' }); // $ExpectType HTMLElement +createHtmlElement({ name: 'foo', html: '🦄', text: 'Hello World' }); // $ExpectError diff --git a/types/create-html-element/index.d.ts b/types/create-html-element/index.d.ts new file mode 100644 index 0000000000..86fd758b3e --- /dev/null +++ b/types/create-html-element/index.d.ts @@ -0,0 +1,40 @@ +// Type definitions for create-html-element 2.0 +// Project: https://github.com/sindresorhus/create-html-element#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +export = createHtmlElement; + +declare function createHtmlElement( + options?: createHtmlElement.OptionsWithoutTagName +): HTMLDivElement; +declare function createHtmlElement( + options: createHtmlElement.Options +): HTMLElementTagNameMap[K]; +declare function createHtmlElement(options: createHtmlElement.Options): HTMLElement; + +declare namespace createHtmlElement { + type Without = { [P in Exclude]?: never }; + type XOR = (T | U) extends object ? (Without & U) | (Without & T) : T | U; + + interface AttributesOptions { + attributes?: { [key: string]: string | boolean | number | string[] }; + } + + interface HtmlOptions { + html?: string; + } + + interface TextOptions { + text?: string; + } + + type OptionsWithoutTagName = AttributesOptions & XOR; + + interface OptionsWithTagName extends AttributesOptions { + name: K; + } + + type Options = OptionsWithTagName & XOR; +} diff --git a/types/create-html-element/package.json b/types/create-html-element/package.json new file mode 100644 index 0000000000..bb80441dc0 --- /dev/null +++ b/types/create-html-element/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "types": "index", + "typesVersions": { + ">=3.1.0-0": { "*": ["ts3.1/*"] } + } +} diff --git a/types/create-html-element/ts3.1/create-html-element-tests.ts b/types/create-html-element/ts3.1/create-html-element-tests.ts new file mode 100644 index 0000000000..6d44f3df60 --- /dev/null +++ b/types/create-html-element/ts3.1/create-html-element-tests.ts @@ -0,0 +1,64 @@ +import createHtmlElement = require('create-html-element'); + +createHtmlElement(); // $ExpectType HTMLDivElement +// $ExpectType HTMLDivElement +createHtmlElement({ + attributes: { + class: 'unicorn', + rainbow: true, + horse: false, + number: 1, + multiple: ['a', 'b'], + }, +}); +createHtmlElement({ html: '🦄' }); // $ExpectType HTMLDivElement +createHtmlElement({ text: 'Hello World' }); // $ExpectType HTMLDivElement +createHtmlElement({ html: '🦄', text: 'Hello World' }); // $ExpectError + +createHtmlElement({ name: 'h1' }); // $ExpectType HTMLHeadingElement +// $ExpectType HTMLHeadingElement +createHtmlElement({ + name: 'h1', + attributes: { + class: 'unicorn', + rainbow: true, + horse: false, + number: 1, + multiple: ['a', 'b'], + }, +}); +createHtmlElement({ name: 'h1', html: '🦄' }); // $ExpectType HTMLHeadingElement +createHtmlElement({ name: 'h1', text: 'Hello World' }); // $ExpectType HTMLHeadingElement +createHtmlElement({ name: 'h1', html: '🦄', text: 'Hello World' }); // $ExpectError + +createHtmlElement({ name: 'listing' }); // $ExpectType HTMLPreElement +// $ExpectType HTMLPreElement +createHtmlElement({ + name: 'listing', + attributes: { + class: 'unicorn', + rainbow: true, + horse: false, + number: 1, + multiple: ['a', 'b'], + }, +}); +createHtmlElement({ name: 'listing', html: '🦄' }); // $ExpectType HTMLPreElement +createHtmlElement({ name: 'listing', text: 'Hello World' }); // $ExpectType HTMLPreElement +createHtmlElement({ name: 'listing', html: '🦄', text: 'Hello World' }); // $ExpectError + +createHtmlElement({ name: 'foo' }); // $ExpectType HTMLElement +// $ExpectType HTMLElement +createHtmlElement({ + name: 'foo', + attributes: { + class: 'unicorn', + rainbow: true, + horse: false, + number: 1, + multiple: ['a', 'b'], + }, +}); +createHtmlElement({ name: 'foo', html: '🦄' }); // $ExpectType HTMLElement +createHtmlElement({ name: 'foo', text: 'Hello World' }); // $ExpectType HTMLElement +createHtmlElement({ name: 'foo', html: '🦄', text: 'Hello World' }); // $ExpectError diff --git a/types/create-html-element/ts3.1/index.d.ts b/types/create-html-element/ts3.1/index.d.ts new file mode 100644 index 0000000000..113f9ba588 --- /dev/null +++ b/types/create-html-element/ts3.1/index.d.ts @@ -0,0 +1,38 @@ +export = createHtmlElement; + +declare function createHtmlElement( + options?: createHtmlElement.OptionsWithoutTagName +): HTMLDivElement; +declare function createHtmlElement( + options: createHtmlElement.Options +): HTMLElementTagNameMap[K]; +/** @deprecated */ +declare function createHtmlElement( + options: createHtmlElement.Options +): HTMLElementDeprecatedTagNameMap[K]; +declare function createHtmlElement(options: createHtmlElement.Options): HTMLElement; + +declare namespace createHtmlElement { + type Without = { [P in Exclude]?: never }; + type XOR = (T | U) extends object ? (Without & U) | (Without & T) : T | U; + + interface AttributesOptions { + attributes?: { [key: string]: string | boolean | number | string[] }; + } + + interface HtmlOptions { + html?: string; + } + + interface TextOptions { + text?: string; + } + + type OptionsWithoutTagName = AttributesOptions & XOR; + + interface OptionsWithTagName extends AttributesOptions { + name: K; + } + + type Options = OptionsWithTagName & XOR; +} diff --git a/types/create-html-element/ts3.1/tsconfig.json b/types/create-html-element/ts3.1/tsconfig.json new file mode 100644 index 0000000000..6c82d9de10 --- /dev/null +++ b/types/create-html-element/ts3.1/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "create-html-element-tests.ts" + ] +} diff --git a/types/create-html-element/ts3.1/tslint.json b/types/create-html-element/ts3.1/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/create-html-element/ts3.1/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/create-html-element/tsconfig.json b/types/create-html-element/tsconfig.json new file mode 100644 index 0000000000..44a7146986 --- /dev/null +++ b/types/create-html-element/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "create-html-element-tests.ts" + ] +} diff --git a/types/create-html-element/tslint.json b/types/create-html-element/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/create-html-element/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }