[semantic-ui-form] Different weak-type workaround.

This commit is contained in:
Leonard Thieu
2017-06-30 12:09:04 -04:00
parent c8cea81010
commit 40eef89e46
2 changed files with 132 additions and 51 deletions

View File

@@ -55,19 +55,45 @@ declare namespace SemanticUI {
*/
(behavior: 'add errors', errors: string[]): JQuery;
(behavior: 'destroy'): JQuery;
<K extends keyof FormSettings>(behavior: 'setting', name: K, value?: undefined): FormSettings[K];
<K extends keyof FormSettings>(behavior: 'setting', name: K, value: FormSettings[K]): JQuery;
(behavior: 'setting', value: FormSettings.Param): JQuery;
(settings?: FormSettings.Param): JQuery;
<K extends keyof FormSettings>(behavior: 'setting', name: K, value?: undefined): FormSettings._Impl[K];
<K extends keyof FormSettings>(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<FormSettings._Impl, keyof FormSettings._Impl> { }
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<Pick<_Impl, keyof _Impl>>;
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<HTMLElement>, fields: any): void;
/**
* Callback if any form field is invalid
*/
@@ -237,10 +263,12 @@ declare namespace SemanticUI {
prompt: string;
}
interface TextSettings extends Pick<TextSettings._Impl, keyof TextSettings._Impl> { }
type TextSettings = TextSettings.Param;
namespace TextSettings {
type Param = TextSettings | object;
type Param = (Pick<_Impl, 'unspecifiedRule'> |
Pick<_Impl, 'unspecifiedField'>) &
Partial<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**
@@ -254,10 +282,36 @@ declare namespace SemanticUI {
}
}
interface PromptSettings extends Pick<PromptSettings._Impl, keyof PromptSettings._Impl> { }
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<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**
@@ -367,10 +421,12 @@ declare namespace SemanticUI {
}
}
interface TemplatesSettings extends Pick<TemplatesSettings._Impl, keyof TemplatesSettings._Impl> { }
type TemplatesSettings = TemplatesSettings.Param;
namespace TemplatesSettings {
type Param = TemplatesSettings | object;
type Param = (Pick<_Impl, 'error'> |
Pick<_Impl, 'prompt'>) &
Partial<Pick<_Impl, keyof _Impl>>;
interface _Impl {
error(errors: string[]): JQuery;
@@ -378,10 +434,16 @@ declare namespace SemanticUI {
}
}
interface SelectorSettings extends Pick<SelectorSettings._Impl, keyof SelectorSettings._Impl> { }
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<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**
@@ -411,10 +473,11 @@ declare namespace SemanticUI {
}
}
interface MetadataSettings extends Pick<MetadataSettings._Impl, keyof MetadataSettings._Impl> { }
type MetadataSettings = MetadataSettings.Param;
namespace MetadataSettings {
type Param = MetadataSettings | object;
type Param = (Pick<_Impl, 'validate'>) &
Partial<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**
@@ -424,10 +487,14 @@ declare namespace SemanticUI {
}
}
interface ClassNameSettings extends Pick<ClassNameSettings._Impl, keyof ClassNameSettings._Impl> { }
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<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**
@@ -449,10 +516,11 @@ declare namespace SemanticUI {
}
}
interface ErrorSettings extends Pick<ErrorSettings._Impl, keyof ErrorSettings._Impl> { }
type ErrorSettings = ErrorSettings.Param;
namespace ErrorSettings {
type Param = ErrorSettings | object;
type Param = (Pick<_Impl, 'method'>) &
Partial<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**

View File

@@ -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<HTMLElement>
$(selector).form('is valid'); // $ExpectType boolean
$(selector).form('validate form'); // $ExpectType JQuery<HTMLElement>
$(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<HTMLElement>
$(selector).form('get value', 'id'); // $ExpectType any
$(selector).form('get values', ['id1', 'id2']); // $ExpectType any
$(selector).form('set value', 'id'); // $ExpectType JQuery<HTMLElement>
$(selector).form('set values', ['id1', 'id2']); // $ExpectType JQuery<HTMLElement>
$(selector).form('get validation', $()); // $ExpectType any
$(selector).form('has field', 'identifier'); // $ExpectType boolean
$(selector).form('add errors', ['error1', 'error2']); // $ExpectType JQuery<HTMLElement>
$(selector).form('destroy'); // $ExpectType JQuery<HTMLElement>
$(selector).form('setting', 'debug', undefined); // $ExpectType boolean
$(selector).form('setting', 'debug'); // $ExpectType boolean
$(selector).form('setting', 'debug', true); // $ExpectType JQuery<HTMLElement>
// $ExpectType JQuery<HTMLElement>
$(selector).form('setting', {
namespace: 'namespace',
name: 'name',
@@ -33,12 +34,13 @@ function test_form() {
debug: true,
performance: true,
verbose: true
}) === $();
});
// $ExpectType JQuery<HTMLElement>
$(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<HTMLElement>
},
onInvalid() {
this === $();
this; // $ExpectType JQuery<HTMLElement>
},
onSuccess(event, fields) {
this === $();
event.stopPropagation();
fields === {};
this; // $ExpectType JQuery<HTMLElement>
event; // $ExpectType Event<HTMLElement, null>
fields; // $ExpectType any
},
onFailure(formErrors, fields) {
this === $();
formErrors === [];
fields === {};
this; // $ExpectType JQuery<HTMLElement>
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<HTMLElement>
$(selector).form('foo'); // $ExpectError
$(selector).form({ foo: 'bar' }); // $ExpectError
}
import form = require('semantic-ui-form');
function test_module() {
form; // $ExpectType Form
$.fn.form = form;
}