From 40eef89e465de56ba3daa9eaa8edb449344c0e57 Mon Sep 17 00:00:00 2001 From: Leonard Thieu Date: Fri, 30 Jun 2017 12:09:04 -0400 Subject: [PATCH] [semantic-ui-form] Different weak-type workaround. --- types/semantic-ui-form/global.d.ts | 110 ++++++++++++++---- .../semantic-ui-form-tests.ts | 73 +++++++----- 2 files changed, 132 insertions(+), 51 deletions(-) diff --git a/types/semantic-ui-form/global.d.ts b/types/semantic-ui-form/global.d.ts index 13fefb8147..3f8b3977e9 100644 --- a/types/semantic-ui-form/global.d.ts +++ b/types/semantic-ui-form/global.d.ts @@ -55,19 +55,45 @@ declare namespace SemanticUI { */ (behavior: 'add errors', errors: string[]): JQuery; (behavior: 'destroy'): JQuery; - (behavior: 'setting', name: K, value?: undefined): FormSettings[K]; - (behavior: 'setting', name: K, value: FormSettings[K]): JQuery; - (behavior: 'setting', value: FormSettings.Param): JQuery; - (settings?: FormSettings.Param): JQuery; + (behavior: 'setting', name: K, value?: undefined): FormSettings._Impl[K]; + (behavior: 'setting', name: K, value: FormSettings._Impl[K]): JQuery; + (behavior: 'setting', value: FormSettings): JQuery; + (settings?: FormSettings): JQuery; } /** * @see {@link http://semantic-ui.com/behaviors/form.html#/settings} */ - interface FormSettings extends Pick { } + type FormSettings = FormSettings.Param; namespace FormSettings { - type Param = FormSettings | object; + type Param = (Pick<_Impl, 'keyboardShortcuts'> | + Pick<_Impl, 'on'> | + Pick<_Impl, 'revalidate'> | + Pick<_Impl, 'delay'> | + Pick<_Impl, 'inline'> | + Pick<_Impl, 'transition'> | + Pick<_Impl, 'duration'> | + Pick<_Impl, 'fields'> | + Pick<_Impl, 'text'> | + Pick<_Impl, 'prompt'> | + Pick<_Impl, 'onValid'> | + Pick<_Impl, 'onInvalid'> | + Pick<_Impl, 'onSuccess'> | + Pick<_Impl, 'onFailure'> | + Pick<_Impl, 'templates'> | + Pick<_Impl, 'rules'> | + Pick<_Impl, 'selector'> | + Pick<_Impl, 'metadata'> | + Pick<_Impl, 'className'> | + Pick<_Impl, 'error'> | + Pick<_Impl, 'namespace'> | + Pick<_Impl, 'name'> | + Pick<_Impl, 'silent'> | + Pick<_Impl, 'debug'> | + Pick<_Impl, 'performance'> | + Pick<_Impl, 'verbose'>) & + Partial>; interface _Impl { // region Form Settings @@ -141,7 +167,7 @@ declare namespace SemanticUI { /** * Callback if a form is all valid */ - onSuccess(this: JQuery, event: JQueryEventObject, fields: any): void; + onSuccess(this: JQuery, event: JQuery.Event, fields: any): void; /** * Callback if any form field is invalid */ @@ -237,10 +263,12 @@ declare namespace SemanticUI { prompt: string; } - interface TextSettings extends Pick { } + type TextSettings = TextSettings.Param; namespace TextSettings { - type Param = TextSettings | object; + type Param = (Pick<_Impl, 'unspecifiedRule'> | + Pick<_Impl, 'unspecifiedField'>) & + Partial>; interface _Impl { /** @@ -254,10 +282,36 @@ declare namespace SemanticUI { } } - interface PromptSettings extends Pick { } + type PromptSettings = PromptSettings.Param; namespace PromptSettings { - type Param = PromptSettings | object; + type Param = (Pick<_Impl, 'empty'> | + Pick<_Impl, 'checked'> | + Pick<_Impl, 'email'> | + Pick<_Impl, 'url'> | + Pick<_Impl, 'regExp'> | + Pick<_Impl, 'integer'> | + Pick<_Impl, 'decimal'> | + Pick<_Impl, 'number'> | + Pick<_Impl, 'is'> | + Pick<_Impl, 'isExactly'> | + Pick<_Impl, 'not'> | + Pick<_Impl, 'notExactly'> | + Pick<_Impl, 'contain'> | + Pick<_Impl, 'containExactly'> | + Pick<_Impl, 'doesntContain'> | + Pick<_Impl, 'doesntContainExactly'> | + Pick<_Impl, 'minLength'> | + Pick<_Impl, 'length'> | + Pick<_Impl, 'exactLength'> | + Pick<_Impl, 'maxLength'> | + Pick<_Impl, 'match'> | + Pick<_Impl, 'different'> | + Pick<_Impl, 'creditCard'> | + Pick<_Impl, 'minCount'> | + Pick<_Impl, 'exactCount'> | + Pick<_Impl, 'maxCount'>) & + Partial>; interface _Impl { /** @@ -367,10 +421,12 @@ declare namespace SemanticUI { } } - interface TemplatesSettings extends Pick { } + type TemplatesSettings = TemplatesSettings.Param; namespace TemplatesSettings { - type Param = TemplatesSettings | object; + type Param = (Pick<_Impl, 'error'> | + Pick<_Impl, 'prompt'>) & + Partial>; interface _Impl { error(errors: string[]): JQuery; @@ -378,10 +434,16 @@ declare namespace SemanticUI { } } - interface SelectorSettings extends Pick { } + type SelectorSettings = SelectorSettings.Param; namespace SelectorSettings { - type Param = SelectorSettings | object; + type Param = (Pick<_Impl, 'message'> | + Pick<_Impl, 'field'> | + Pick<_Impl, 'group'> | + Pick<_Impl, 'input'> | + Pick<_Impl, 'prompt'> | + Pick<_Impl, 'submit'>) & + Partial>; interface _Impl { /** @@ -411,10 +473,11 @@ declare namespace SemanticUI { } } - interface MetadataSettings extends Pick { } + type MetadataSettings = MetadataSettings.Param; namespace MetadataSettings { - type Param = MetadataSettings | object; + type Param = (Pick<_Impl, 'validate'>) & + Partial>; interface _Impl { /** @@ -424,10 +487,14 @@ declare namespace SemanticUI { } } - interface ClassNameSettings extends Pick { } + type ClassNameSettings = ClassNameSettings.Param; namespace ClassNameSettings { - type Param = ClassNameSettings | object; + type Param = (Pick<_Impl, 'active'> | + Pick<_Impl, 'placeholder'> | + Pick<_Impl, 'disabled'> | + Pick<_Impl, 'visible'>) & + Partial>; interface _Impl { /** @@ -449,10 +516,11 @@ declare namespace SemanticUI { } } - interface ErrorSettings extends Pick { } + type ErrorSettings = ErrorSettings.Param; namespace ErrorSettings { - type Param = ErrorSettings | object; + type Param = (Pick<_Impl, 'method'>) & + Partial>; interface _Impl { /** diff --git a/types/semantic-ui-form/semantic-ui-form-tests.ts b/types/semantic-ui-form/semantic-ui-form-tests.ts index 58e8a18f70..56293fc5b4 100644 --- a/types/semantic-ui-form/semantic-ui-form-tests.ts +++ b/types/semantic-ui-form/semantic-ui-form-tests.ts @@ -1,5 +1,5 @@ function test_form_static() { - $.fn.form.settings.error.method = 'method'; + $.fn.form.settings.error!.method = 'method'; $.fn.form.settings.namespace = 'namespace'; $.fn.form.settings.name = 'name'; $.fn.form.settings.silent = false; @@ -10,22 +10,23 @@ function test_form_static() { function test_form() { const selector = '.ui.form'; - $(selector).form('submit') === $(); - $(selector).form('is valid') === false; - $(selector).form('validate form') === $(); + $(selector).form('submit'); // $ExpectType JQuery + $(selector).form('is valid'); // $ExpectType boolean + $(selector).form('validate form'); // $ExpectType JQuery $(selector).form('get change event') === 'change event'; - $(selector).form('get field', 'id') === $(); - $(selector).form('get value', 'id') === {}; - $(selector).form('get values', ['id1', 'id2']) === {}; - $(selector).form('set value', 'id') === $(); - $(selector).form('set values', ['id1', 'id2']) === $(); - $(selector).form('get validation', $()) === {}; - $(selector).form('has field', 'identifier') === true; - $(selector).form('add errors', ['error1', 'error2']) === $(); - $(selector).form('destroy') === $(); - $(selector).form('setting', 'debug', undefined) === false; - $(selector).form('setting', 'debug') === false; - $(selector).form('setting', 'debug', true) === $(); + $(selector).form('get field', 'id'); // $ExpectType JQuery + $(selector).form('get value', 'id'); // $ExpectType any + $(selector).form('get values', ['id1', 'id2']); // $ExpectType any + $(selector).form('set value', 'id'); // $ExpectType JQuery + $(selector).form('set values', ['id1', 'id2']); // $ExpectType JQuery + $(selector).form('get validation', $()); // $ExpectType any + $(selector).form('has field', 'identifier'); // $ExpectType boolean + $(selector).form('add errors', ['error1', 'error2']); // $ExpectType JQuery + $(selector).form('destroy'); // $ExpectType JQuery + $(selector).form('setting', 'debug', undefined); // $ExpectType boolean + $(selector).form('setting', 'debug'); // $ExpectType boolean + $(selector).form('setting', 'debug', true); // $ExpectType JQuery + // $ExpectType JQuery $(selector).form('setting', { namespace: 'namespace', name: 'name', @@ -33,12 +34,13 @@ function test_form() { debug: true, performance: true, verbose: true - }) === $(); + }); + // $ExpectType JQuery $(selector).form({ keyboardShortcuts: false, on: 'submit', revalidate: true, - delay: 20, + delay: false, inline: false, transition: 'fade', duration: 22, @@ -92,36 +94,43 @@ function test_form() { maxCount: 'maxCount' }, onValid() { - this === $(); + this; // $ExpectType JQuery }, onInvalid() { - this === $(); + this; // $ExpectType JQuery }, onSuccess(event, fields) { - this === $(); - event.stopPropagation(); - fields === {}; + this; // $ExpectType JQuery + event; // $ExpectType Event + fields; // $ExpectType any }, onFailure(formErrors, fields) { - this === $(); - formErrors === []; - fields === {}; + this; // $ExpectType JQuery + formErrors; // $ExpectType string[] + fields; // $ExpectType any }, templates: { error(errors) { - errors === []; + errors; // $ExpectType string[] + return $(); }, prompt(errors) { - errors === []; + errors; // $ExpectType string[] + return $(); } }, rules: { empty(value) { + this; // $ExpectType HTMLElement + value; // $ExpectType any + return !(value === undefined || '' === value || $.isArray(value) && value.length === 0); }, checked() { + this; // $ExpectType HTMLElement + return ($(this).filter(':checked').length > 0); } }, @@ -145,12 +154,16 @@ function test_form() { error: { method: 'method' } - }) === $(); - $(selector).form() === $(); + }); + $(selector).form(); // $ExpectType JQuery + + $(selector).form('foo'); // $ExpectError + $(selector).form({ foo: 'bar' }); // $ExpectError } import form = require('semantic-ui-form'); function test_module() { + form; // $ExpectType Form $.fn.form = form; }