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

Fix typing of the Observable<boolean> #168

Open
brianmhunt opened this issue Mar 7, 2022 · 5 comments
Open

Fix typing of the Observable<boolean> #168

brianmhunt opened this issue Mar 7, 2022 · 5 comments
Labels

Comments

@brianmhunt
Copy link
Member

See:

// Broken
function observable<T>(value: T): Observable<T>
{
  return undefined as any;  // the implementation is not important
}

// Fixed
function observable<T>(value: T extends infer U ? U: never): Observable<T>
{
  return undefined as any;  // the implementation is not important
}
@maskmaster
Copy link

I added an answer in the stackoverflow question. We need to make sure that these two lines remain valid after any fix.

const x = observable<false | undefined>(false);
const y = observable(false);

@tomhanax
Copy link

tomhanax commented Mar 8, 2022

For now the best I can offer is:

// Fixed
function observable<T, U = any>(value?: T extends infer R ? R : U): unknown extends T ? Observable<U> : Observable<T>
{
  return undefined as any;  // the implementation is not important
}

The playground contains many examples and seems OK, of course anybody can check and correct if needed, I am no expert in this.

Playground Link

@brianmhunt
Copy link
Member Author

Thanks @maskmaster , @tomhanax the answer & playground are very helpful.

direct link to the SO question

@chuanqisun
Copy link

Hi, I believe I'm hitting the same issue when upgrading to typescript 4.6 but I might need some help understanding the workaround. Here is a minimum repro.

image

The ExtendedType is clearly assignable to the BaseType, but typescript wouldn't allow. Also, the error suggests that typescript is trying to assign BaseType to ExtendedType, which is opposite to the desired direction.

Should I manually patch the type definitely of KnockoutObservable? I'm not sure how that might work. Thank you and let me know if I should cross post to other repos.

@mbest
Copy link
Member

mbest commented Jul 15, 2022

This was fixed in TypeScript: microsoft/TypeScript#48380

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants