-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Narrow types of observables in conditions #132
Comments
Simply unwrapping the observable won't work because the observable is a function. According to typescript, functions are nondeterministic, meaning that we can't expect the same return value. const foo = ko.observable<string | undefined>();
if (foo() !== undefined) {
const bar = foo();
bar // string | undefined
} The if/ifnot bindings should return a new child context where it extracts all the truthy values from the observable. declare function binding<T>(value: ko.Observable<T>): ko.Observable<Extract<T, {}>>;
const foo = ko.observable<string | undefined>();
const bar = binding(foo); // extracts thruthy values
const baz = bar();
baz // string |
This is a problem because we can't know what condition is passed to the if/ifnot bindings. In the below example, we expect name to be <!-- ko if: name() !== null -->
Hello <span data-bind="text: name"></span>!
<!-- /ko --> The below example unfortunately does not work since foo will only recieve an overload. So the type will become const foo = ko.observable<string | undefined>();
assert<ko.Observable<string>>(foo);
const bar = foo();
bar // string | undefined A solution to this would be to assert all instances of the observable in the descendants of the binding. The downside is that this would probably require a lot of work and slow down the transpiler even more. const foo = ko.observable<string | undefined>();
const bar = foo() as string;
bar // string Issue is open to suggestions! |
The text was updated successfully, but these errors were encountered: