microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.61k stars 12.44k forks source link

Class expressions should support `in` and `out` variance modifiers #48618

Closed DanielRosenwasser closed 2 years ago

DanielRosenwasser commented 2 years ago

Likely an oversight but

let Anon = class <out T> {
    foo(): InstanceType<(typeof Anon<T>)> {
        return this;
    }
}

let OuterC = class C<out T> {
    foo(): C<T> {
        return this;
    }
}

Results in the following message:

error! 'out' modifier can only appear on a type parameter of a class, interface or type alias
ahejlsberg commented 2 years ago

Yup, that's an oversight.

ahejlsberg commented 2 years ago

The repro actually reveals another problem: Validation of variance annotations needs to be deferred as it may otherwise cause circularity errors. Indeed, the Anon example errors if validation isn't deferred. I'll put up a PR that fixes both of these issues.