You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, I expect TypeScript to deduce the type for result to be number. However, it deduces the type to be any. This can be attributed to the fact that the base type (SubscribableFunctions<T>) extends Function, which discards the type parameter.
This can be fixed by modifying the Subscribable<T> type declaration to reflect Observable<T>:
The text was updated successfully, but these errors were encountered:
craxal
changed the title
Type definition does not provide correct return type retrieving Subscribable value
Type information lost when retrieving Subscribable value
Dec 12, 2020
craxal
changed the title
Type information lost when retrieving Subscribable value
Type information is lost when retrieving Subscribable value
Dec 12, 2020
You have to define the type of the observable as follows
import * as ko from "knockout";
let obj = ko.observable<number>(5);
let result = obj();
Then TypeScript will infer type of result as number
craxal
changed the title
Type information is lost when retrieving Subscribable value
Type information is lost when retrieving Subscribable<T> value
Jan 4, 2021
@sumitkm Did a little more digging, and I've updated the example to more closely resemble the situation we're running into. There is no problem with Observable<T> type, but that's not the type I'm concerned about. There is a problem with Subscribable<T>. This is a type we use, because there are cases where we want to use an observable or a computed, and Subscribable<T> is their common base type.
Consider the following code:
Here, I expect TypeScript to deduce the type for
result
to benumber
. However, it deduces the type to beany
. This can be attributed to the fact that the base type (SubscribableFunctions<T>
) extendsFunction
, which discards the type parameter.This can be fixed by modifying the
Subscribable<T>
type declaration to reflectObservable<T>
:The text was updated successfully, but these errors were encountered: