Skip to content
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

Type information is lost when retrieving Subscribable<T> value #2555

Open
craxal opened this issue Dec 12, 2020 · 2 comments
Open

Type information is lost when retrieving Subscribable<T> value #2555

craxal opened this issue Dec 12, 2020 · 2 comments
Milestone

Comments

@craxal
Copy link

craxal commented Dec 12, 2020

Consider the following code:

import * as ko from "knockout";
let obj: ko.Subscribable<number> = ko.observable(5);
let result = obj();

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>:

export interface Subscribable<T = any> extends SubscribableFunctions<T> {
    (): T
}
@craxal 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 craxal changed the title Type information lost when retrieving Subscribable value Type information is lost when retrieving Subscribable value Dec 12, 2020
@sumitkm
Copy link

sumitkm commented Dec 24, 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 craxal changed the title Type information is lost when retrieving Subscribable value Type information is lost when retrieving Subscribable<T> value Jan 4, 2021
@craxal
Copy link
Author

craxal commented 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.

@mbest mbest added this to the 3.5.2 milestone Feb 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants