TypeScript/tests/baselines/reference/enumPropertyAccess.types
Anders Hejlsberg 15fae38b39
Improve narrowing of generic types in control flow analysis (#43183)
* Narrow type variables with union constraints when merited by contextual type

* Narrow generics with union type constraints as indicated by contextual type

* Accept new baselines

* Add tests

* Fix circularity for JSX elements

* Remove unnecessary isConstraintPosition information from flow cache key

* Update comment

* Add additional tests

* Rename to getNarrowableTypeForReference, remove getConstraintForLocation

* Add comment

* Fix removal of undefined in destructurings with initializers

* Use getContextFreeTypeOfExpression in discriminateContextualTypeByObjectMembers

* In obj[x], use constraint of obj's type only when x's type is non-generic

* Add comment
2021-03-19 17:12:57 -07:00

46 lines
887 B
Plaintext

=== tests/cases/compiler/enumPropertyAccess.ts ===
enum Colors {
>Colors : Colors
Red,
>Red : Colors.Red
Green
>Green : Colors.Green
}
var x = Colors.Red; // type of 'x' should be 'Colors'
>x : Colors
>Colors.Red : Colors.Red
>Colors : typeof Colors
>Red : Colors.Red
var p = x.Green; // error
>p : any
>x.Green : any
>x : Colors.Red
>Green : any
x.toFixed(); // ok
>x.toFixed() : string
>x.toFixed : (fractionDigits?: number) => string
>x : Colors.Red
>toFixed : (fractionDigits?: number) => string
// Now with generics
function fill<B extends Colors>(f: B) {
>fill : <B extends Colors>(f: B) => void
>f : B
f.Green; // error
>f.Green : any
>f : Colors
>Green : any
f.toFixed(); // ok
>f.toFixed() : string
>f.toFixed : (fractionDigits?: number) => string
>f : Colors
>toFixed : (fractionDigits?: number) => string
}