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

This commit is contained in:
Leonard Thieu
2017-06-29 17:14:00 -04:00
parent 54711afc67
commit a94cdc51ca
2 changed files with 90 additions and 45 deletions
+45 -14
View File
@@ -97,17 +97,41 @@ declare namespace SemanticUI {
(behavior: 'destroy'): JQuery;
<K extends keyof CheckboxSettings>(behavior: 'setting', name: K, value?: undefined): CheckboxSettings[K];
<K extends keyof CheckboxSettings>(behavior: 'setting', name: K, value: CheckboxSettings[K]): JQuery;
(behavior: 'setting', value: CheckboxSettings.Param): JQuery;
(settings?: CheckboxSettings.Param): JQuery;
(behavior: 'setting', value: CheckboxSettings): JQuery;
(settings?: CheckboxSettings): JQuery;
}
/**
* @see {@link http://semantic-ui.com/modules/checkbox.html#/settings}
*/
interface CheckboxSettings extends Pick<CheckboxSettings._Impl, keyof CheckboxSettings._Impl> { }
type CheckboxSettings = CheckboxSettings.Param;
namespace CheckboxSettings {
type Param = CheckboxSettings | object;
type Param = (Pick<_Impl, 'uncheckable'> |
Pick<_Impl, 'fireOnInit'> |
Pick<_Impl, 'onChange'> |
Pick<_Impl, 'onChecked'> |
Pick<_Impl, 'onIndeterminate'> |
Pick<_Impl, 'onDeterminate'> |
Pick<_Impl, 'onUnchecked'> |
Pick<_Impl, 'beforeChecked'> |
Pick<_Impl, 'beforeIndeterminate'> |
Pick<_Impl, 'beforeDeterminate'> |
Pick<_Impl, 'beforeUnchecked'> |
Pick<_Impl, 'onEnable'> |
Pick<_Impl, 'onDisable'> |
Pick<_Impl, 'onEnabled'> |
Pick<_Impl, 'onDisabled'> |
Pick<_Impl, 'selector'> |
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 Behavior
@@ -152,19 +176,19 @@ declare namespace SemanticUI {
/**
* Callback before a checkbox is checked. Can cancel change by returning false
*/
beforeChecked(this: HTMLElement): boolean;
beforeChecked(this: HTMLElement): void | false;
/**
* Callback before a checkbox is set to undeterminate. Can cancel change by returning false
*/
beforeIndeterminate(this: HTMLElement): boolean;
beforeIndeterminate(this: HTMLElement): void | false;
/**
* Callback before a checkbox is set to determinate. Can cancel change by returning false
*/
beforeDeterminate(this: HTMLElement): boolean;
beforeDeterminate(this: HTMLElement): void | false;
/**
* Callback before a checkbox is unchecked. Can cancel change by returning false
*/
beforeUnchecked(this: HTMLElement): boolean;
beforeUnchecked(this: HTMLElement): void | false;
/**
* Callback after a checkbox is enabled.
*/
@@ -248,10 +272,12 @@ declare namespace SemanticUI {
}
namespace Checkbox {
interface SelectorSettings extends Pick<SelectorSettings._Impl, keyof SelectorSettings._Impl> { }
type SelectorSettings = SelectorSettings.Param;
namespace SelectorSettings {
type Param = SelectorSettings | object;
type Param = (Pick<_Impl, 'input'> |
Pick<_Impl, 'label'>) &
Partial<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**
@@ -265,10 +291,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, 'checked'> |
Pick<_Impl, 'disabled'> |
Pick<_Impl, 'radio'> |
Pick<_Impl, 'readOnly'>) &
Partial<Pick<_Impl, keyof _Impl>>;
interface _Impl {
/**
@@ -290,10 +320,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 {
/**
@@ -1,5 +1,5 @@
function test_checkbox_static() {
$.fn.checkbox.settings.error.method = 'method';
$.fn.checkbox.settings.error!.method = 'method';
$.fn.checkbox.settings.namespace = 'namespace';
$.fn.checkbox.settings.name = 'name';
$.fn.checkbox.settings.silent = false;
@@ -10,19 +10,19 @@ function test_checkbox_static() {
function test_checkbox() {
const selector = '.ui.checkbox';
$(selector).checkbox('toggle') === $();
$(selector).checkbox('check') === $();
$(selector).checkbox('uncheck') === $();
$(selector).checkbox('indeterminate') === $();
$(selector).checkbox('determinate') === $();
$(selector).checkbox('enable') === $();
$(selector).checkbox('set checked') === $();
$(selector).checkbox('set unchecked') === $();
$(selector).checkbox('set indeterminate') === $();
$(selector).checkbox('set determinate') === $();
$(selector).checkbox('set enabled') === $();
$(selector).checkbox('set disabled') === $();
$(selector).checkbox('attach events', $(), 'click') === $();
$(selector).checkbox('toggle'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('check'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('uncheck'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('indeterminate'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('determinate'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('enable'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('set checked'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('set unchecked'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('set indeterminate'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('set determinate'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('set enabled'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('set disabled'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('attach events', $(), 'click'); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('is radio') === true;
$(selector).checkbox('is checked') === true;
$(selector).checkbox('is unchecked') === true;
@@ -34,7 +34,8 @@ function test_checkbox() {
$(selector).checkbox('can uncheck') === true;
$(selector).checkbox('setting', 'debug', undefined) === false;
$(selector).checkbox('setting', 'debug') === false;
$(selector).checkbox('setting', 'debug', true) === $();
$(selector).checkbox('setting', 'debug', true); // $ExpectType JQuery<HTMLElement>
// $ExpectType JQuery<HTMLElement>
$(selector).checkbox('setting', {
namespace: 'namespace',
name: 'name',
@@ -42,48 +43,57 @@ function test_checkbox() {
debug: true,
performance: true,
verbose: true
}) === $();
});
// $ExpectType JQuery<HTMLElement>
$(selector).checkbox({
uncheckable: false,
fireOnInit: true,
onChange() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
onChecked() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
onIndeterminate() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
onDeterminate() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
onUnchecked() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
beforeChecked() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
return false;
},
beforeIndeterminate() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
return false;
},
beforeDeterminate() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
return false;
},
beforeUnchecked() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
return false;
},
onEnable() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
onDisable() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
onEnabled() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
onDisabled() {
this === new HTMLElement();
this; // $ExpectType HTMLElement
},
selector: {
input: 'input',
@@ -98,12 +108,16 @@ function test_checkbox() {
error: {
method: 'method'
}
}) === $();
$(selector).checkbox() === $();
});
$(selector).checkbox(); // $ExpectType JQuery<HTMLElement>
$(selector).checkbox('foo'); // $ExpectError
$(selector).checkbox({ foo: 'bar' }); // $ExpectError
}
import checkbox = require('semantic-ui-checkbox');
function test_module() {
checkbox; // $ExpectType Checkbox
$.fn.checkbox = checkbox;
}