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

Incorrect Apparent Type of a Union of Constructors #60839

Open
LukeAbby opened this issue Dec 22, 2024 · 0 comments
Open

Incorrect Apparent Type of a Union of Constructors #60839

LukeAbby opened this issue Dec 22, 2024 · 0 comments

Comments

@LukeAbby
Copy link

πŸ”Ž Search Terms

union of constructors, InstanceType, narrowing

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about constructor type narrowing

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=4.9.5#code/CYUwxgNghgTiAEkoGdnwKIA8oFsAOEIAwtKvAN4CwAUPHfHjAPZ4Bc8AdgK44BGIMANw0AvjRoAXAJ54EAFSgBrEMiy4CIADxz4ITBJAdgaNfkIkUyAHzwAvPDnDqkmfJUTtu-YePxpspgAzDGwzYlI0AB94AAoOEAB3WIBKOxsmXgArcAlkm3sdPQMjNH8QIJD1cwiaengAfgclFVMNTQBJDmQJKA4wEDlXbSsrWvoAenG66bqAPQdXeAByTu7e-sHZYaX4YCYVTiYJeGQoCQBLZECpPwALBDAmLokYKHOOY6XW6sslgDoxnRJjMQXVNggllwOIoOEwEhwdpdDsdLOcAOYcKC8Qh+Jh+RZfUIaCyof6A+DA0FUhayZYZbJgY7Rb7hX7wJGwlGodGY7EICR4srLFkk5Bk2gTKbU6kABWYshg0mWjBYiLQOEuyHeaPZHHxtKW5BEO14XGOcAAjlxznBgLr9RCRRFxXV2PEAG4CQRAA

πŸ’» Code

declare class ExampleClass {
    prop: number;
}

type TakesExample<T extends ExampleClass> = T;

type Test<T extends typeof ExampleClass | (new () => object)> = T extends typeof ExampleClass
    ? TakesExample<InstanceType<T>>
    //             ^ Type 'InstanceType<T>' does not satisfy the constraint 'ExampleClass'.
    //                 Type 'unknown' is not assignable to type 'ExampleClass'.
    //                   Type 'object | ExampleClass' is not assignable to type 'ExampleClass'.
    //                     Property 'prop' is missing in type '{}' but required in type 'ExampleClass'.
    : never;

πŸ™ Actual behavior

This errors because it appears to lose the context that T is typeof ExampleClass when ran through InstanceType. I find this especially odd because if you make a TakesExampleClass<T> it'll succeed.

πŸ™‚ Expected behavior

No error.

Additional information about the issue

No response

@LukeAbby LukeAbby changed the title Incorrect Narrowing of a Union of Constructors Incorrect Apparent Type of a Union of Constructors Dec 22, 2024
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

1 participant