Skip to content

Polymorphic this in static methods is not resolving correctlyΒ #58492

Closed
@gund

Description

@gund

πŸ”Ž Search Terms

polymorphic this in static methods

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgMJgPZQDwBUB8yA3gFDLnIAOUGmYAnpRAFzK4DcJAviQ08gEkQMaOiy5GEPIQC8bZBAAekEABMAzmkw5QIqMgBKhAPyHkrEBABu0TiRIB6B20nqSoSLEQoACjQC2wOoQqBgg6mBQAK4I2sRkFADuwGAAFgYQ6hgANjZQ6tIAFACUrH4YgcEA6inpmTl5BbgANMhpQficPO7g0PBIyOWVUgTxFMjJaRlZudBN+CVlAUEQNVP1s-l4rWI47er4ndz2Hn3eg8vVtdMNc9tsu8hyu9hDK9KHYxTUFSusQnpdhImHhdkdxlANjZClY4Nkoiw2KVkFYMMBVJwIRAAFYQWKFSFwLIgVhREAAaxAGESIGRqPRXXsCGyRM0qCiEQqb2C0gUyggak03JGhFI4wQHMw-hKxGQPG6TmQAFFFHB-JRshASOzOf5hQA6SZ1GaNEr6n7DfUS3UldjIRXYAC0juQAFUAHJKgAaPiVqFwSoAIhdfsFQuFIjE4i7-JLkAAjFB8CAYGBoSVcy5ayyJdO64WFGUyQggKLZbLFQ3XKFzM0WlZWjPS4p2h3O5BVLDkzRhZCgCJwEADfwQNIYDQkIA

πŸ’» Code

interface Ctor<T> {
    prototype: T;
}
type InferCtorType<T> = T extends Ctor<infer R> ? R : never;


// Types
interface PromiseConstructor {
    withResolvers<T>(): PromiseWithResolvers<T, this>;
}

interface Promise<T> {
    withResolvers<T>(): PromiseWithResolvers<T, Ctor<this>>;
}

interface PromiseWithResolvers<T, TCtor = Ctor<Promise<T>>> {
    promise: InferCtorType<TCtor>;
    resolve(value: T): void;
    reject(reason: unknown): void;
}

class CustomPromise<T> extends Promise<T> {
    custom() { }
}

// Example
CustomPromise.withResolvers().promise.custom(); // <-- UNEXPECTED PromiseConstructor - must be typeof CustomPromise
new CustomPromise(() => null).withResolvers().promise.custom(); // <-- Works on instance methods

πŸ™ Actual behavior

Polymorphic "this" in static methods is not resolved to actual type but instead resolves to definition time type.

πŸ™‚ Expected behavior

Polymorphic "this" in static methods must resolve to proper subclass depending on the invocation.

Additional information about the issue

There was a similar bug reported #5863 but it is not talking specifically about the "this" type keyword which has the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions