-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Can't infer param type of method assigned to computed property name using string enum value #20856
Comments
NOTE: The mapped type enum Direction {
LTR = "ltr",
RTL = "rtl"
}
interface DirectionVisitor {
ltr: (value: Direction.LTR) => Direction;
rtl: (value: Direction.RTL) => Direction;
}
// "identity" visitor that just returns the visited value
const visitor: DirectionVisitor = {
// ERROR: Parameter 'value' implicitly has type 'any'.
[Direction.LTR]: (value) => {
return value
},
// no error - type of param properly inferred a type Direction.RTL
["rtl"]: (value) => {
return value;
}
} |
(Edit: I figured out how to switch VSCode to use the package-level TypeScript version. I can now confirm with full confidence that the "next" version of TypeScript solves the issue I reported) |
TypeScript Version: 2.6.2
Code
With all strict options enabled...
Expected behavior:
No compiler error. The type of the param of the
[Direction.LTR]
method should be inferred as typeDirection.LTR
.Actual behavior:
Compiler error because the type of the param of the
[Direction.LTR]
method is not inferred, so it is implicitlyany
.It is interesting to me that I can use the enum values as computed property names to fulfill the requirements of the StringVisitor<Direction, Direction> type if I DON'T use parameters:
Or if I explicitly provide the param type, everything lines up and compiles just fine:
The [Direction.LTR] and [Direction.RTL] property names seem to be understood as compile-time string literals for the purpose of matching up against the properties of the StringVisitor<Direction, Direction> type, and the distinct signatures of each property is known by the compiler for the purpose of checking that I provide a function of the correct type... so why can't the compiler infer the type of the param if I omit the param type?
NOTE: the same issue exists even if I declare the enum as a
const enum
.The text was updated successfully, but these errors were encountered: