Add types for create-html-element

This commit is contained in:
Dimitri Benin
2018-12-13 14:32:10 +01:00
parent ad5afdf536
commit b394466e1a
9 changed files with 247 additions and 0 deletions
@@ -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 <em>World</em>' }); // $ExpectType HTMLDivElement
createHtmlElement({ html: '🦄', text: 'Hello <em>World</em>' }); // $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 <em>World</em>' }); // $ExpectType HTMLHeadingElement
createHtmlElement({ name: 'h1', html: '🦄', text: 'Hello <em>World</em>' }); // $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 <em>World</em>' }); // $ExpectType HTMLElement
createHtmlElement({ name: 'foo', html: '🦄', text: 'Hello <em>World</em>' }); // $ExpectError
+40
View File
@@ -0,0 +1,40 @@
// Type definitions for create-html-element 2.0
// Project: https://github.com/sindresorhus/create-html-element#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
export = createHtmlElement;
declare function createHtmlElement(
options?: createHtmlElement.OptionsWithoutTagName
): HTMLDivElement;
declare function createHtmlElement<K extends keyof HTMLElementTagNameMap>(
options: createHtmlElement.Options<K>
): HTMLElementTagNameMap[K];
declare function createHtmlElement(options: createHtmlElement.Options<string>): HTMLElement;
declare namespace createHtmlElement {
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
interface AttributesOptions {
attributes?: { [key: string]: string | boolean | number | string[] };
}
interface HtmlOptions {
html?: string;
}
interface TextOptions {
text?: string;
}
type OptionsWithoutTagName = AttributesOptions & XOR<HtmlOptions, TextOptions>;
interface OptionsWithTagName<K> extends AttributesOptions {
name: K;
}
type Options<K> = OptionsWithTagName<K> & XOR<HtmlOptions, TextOptions>;
}
+7
View File
@@ -0,0 +1,7 @@
{
"private": true,
"types": "index",
"typesVersions": {
">=3.1.0-0": { "*": ["ts3.1/*"] }
}
}
@@ -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 <em>World</em>' }); // $ExpectType HTMLDivElement
createHtmlElement({ html: '🦄', text: 'Hello <em>World</em>' }); // $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 <em>World</em>' }); // $ExpectType HTMLHeadingElement
createHtmlElement({ name: 'h1', html: '🦄', text: 'Hello <em>World</em>' }); // $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 <em>World</em>' }); // $ExpectType HTMLPreElement
createHtmlElement({ name: 'listing', html: '🦄', text: 'Hello <em>World</em>' }); // $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 <em>World</em>' }); // $ExpectType HTMLElement
createHtmlElement({ name: 'foo', html: '🦄', text: 'Hello <em>World</em>' }); // $ExpectError
+38
View File
@@ -0,0 +1,38 @@
export = createHtmlElement;
declare function createHtmlElement(
options?: createHtmlElement.OptionsWithoutTagName
): HTMLDivElement;
declare function createHtmlElement<K extends keyof HTMLElementTagNameMap>(
options: createHtmlElement.Options<K>
): HTMLElementTagNameMap[K];
/** @deprecated */
declare function createHtmlElement<K extends keyof HTMLElementDeprecatedTagNameMap>(
options: createHtmlElement.Options<K>
): HTMLElementDeprecatedTagNameMap[K];
declare function createHtmlElement(options: createHtmlElement.Options<string>): HTMLElement;
declare namespace createHtmlElement {
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
interface AttributesOptions {
attributes?: { [key: string]: string | boolean | number | string[] };
}
interface HtmlOptions {
html?: string;
}
interface TextOptions {
text?: string;
}
type OptionsWithoutTagName = AttributesOptions & XOR<HtmlOptions, TextOptions>;
interface OptionsWithTagName<K> extends AttributesOptions {
name: K;
}
type Options<K> = OptionsWithTagName<K> & XOR<HtmlOptions, TextOptions>;
}
@@ -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"
]
}
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
+24
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }