Merge pull request #26627 from Retsam/knockout-no-auto-null

Knockout: Remove hardcoded nulls for observables
This commit is contained in:
Paul van Brenk
2018-06-25 17:17:15 -07:00
committed by GitHub
3 changed files with 9 additions and 6 deletions

View File

@@ -106,12 +106,14 @@ interface KnockoutObservableArray<T> extends KnockoutObservable<T[]>, KnockoutOb
interface KnockoutObservableStatic {
fn: KnockoutObservableFunctions<any>;
<T>(value?: T | null): KnockoutObservable<T>;
<T = any>(): KnockoutObservable<T | undefined>
<T = any>(value: null): KnockoutObservable<T | null>
<T>(value: T): KnockoutObservable<T>;
}
interface KnockoutObservable<T> extends KnockoutSubscribable<T>, KnockoutObservableFunctions<T> {
(): T;
(value: T | null): void;
(value: T): void;
peek(): T;
valueHasMutated?:{(): void;};

View File

@@ -575,7 +575,8 @@ describe('Templating', function() {
// Now set the observable to null and check it's treated like an empty array
// (because how else should null be interpreted?)
myArray(null);
// DefinitelyTyped note: while KO accepts this, I wouldn't consider it a well-typed usage of the API
myArray(null); // $ExpectError
expect(testNode.childNodes[0].childNodes.length).toEqual(0);
});
@@ -640,7 +641,7 @@ describe('Templating', function() {
ko.setTemplateEngine(new dummyTemplateEngine({ myTemplate: "Value: [js: myProp().childProp]" }));
testNode.innerHTML = "<div data-bind='template: { name: \"myTemplate\", \"if\": myProp }'></div>";
var viewModel = { myProp: ko.observable({ childProp: 'abc' }) };
var viewModel = { myProp: ko.observable<{childProp: string} | null>({ childProp: 'abc' }) };
ko.applyBindings(viewModel, testNode);
// Initially there is a value
@@ -678,7 +679,7 @@ describe('Templating', function() {
ko.setTemplateEngine(new dummyTemplateEngine({ myTemplate: "Value: [js: myProp().childProp]" }));
testNode.innerHTML = "<div data-bind='template: { name: \"myTemplate\", \"if\": myProp, foreach: [$data, $data, $data] }'></div>";
var viewModel = { myProp: ko.observable({ childProp: 'abc' }) };
var viewModel = { myProp: ko.observable<{childProp: string} | null>({ childProp: 'abc' }) };
ko.applyBindings(viewModel, testNode);
expect(testNode.childNodes[0].childNodes[0].nodeValue).toEqual("Value: abc");
expect(testNode.childNodes[0].childNodes[1].nodeValue).toEqual("Value: abc");

View File

@@ -9,7 +9,7 @@ enum enumTest { male, female }
*/
function ObservableValidationTypes() {
// any
var t0 = ko.observable<any>()
var t0 = ko.observable()
.validate()
.end();