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

Private class properties prevent Readonly type inference #58347

Closed
Trombecher opened this issue Apr 28, 2024 · 2 comments
Closed

Private class properties prevent Readonly type inference #58347

Trombecher opened this issue Apr 28, 2024 · 2 comments

Comments

@Trombecher
Copy link

Trombecher commented Apr 28, 2024

🔎 Search Terms

"type inference readonly private class property", "generic type inference"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about known bugs and type inference.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.0-dev.20240428#code/MYGwhgzhAEBiD28A8AVAfNA3gKGtADgE4CWAbmAC4Cm0A+kfPgPwBc0KA3NgL7bYUBPfDQCSAOwBmVQkljQqAD2piAJjATIwYgWgwBeOPKVVV6xEmKTp7DE3bQ2EsCAhUu2EFQp1qECm3EpGQ0kCkIAVypdLk9vWl8KACYAqxkAJSowFXgxEAFZczDI3TQOaAB6cugkAFpoAAN4qj9E+uhiGHrwsQBreAB3MTbiCThEADp6QkZ2mCIySipxoA

💻 Code

class Foo<T> {
  private _prop?: T;
}

type Infer<F extends Foo<any>> = F extends Foo<infer T> ? T : false;

let _test: Infer<Foo<true>>;
let _test2: Infer<Readonly<Foo<true>>>; // <- `_test2` is `unkown` if Foo._prop is private.

🙁 Actual behavior

The type of the variable _test2 is unknown.

Since Readonly<Foo<T>> extends Foo<T> there is no reason that TypeScript cannot infer T.

🙂 Expected behavior

The type of _test2 should be true or whatever T is (inferred).

Additional information about the issue

If the private modifier from _prop is removed, _test2 correctly shows true or whatever T is. A property modifier should not influence type inference.

@Trombecher
Copy link
Author

nvm Readonly removes private types (???)

@MartinJohns
Copy link
Contributor

See #50668 and others.

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

2 participants