You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now that the left side of a 'for...in' statement is apparently allowed to be keyof the right side, the diagnostic for when the left side has the wrong type needs to be updated to account for that possibility.
(Aside: Is it intentional that the left side won't be inferred to have the keyof type because of the unsoundness described in #12314, but it is allowed to be explicitly declared as that type?)
Code
interfaceFoo{x: number;y: string;}functionscan(f: Foo){letk: number;for(kinf){// Error: "The left-hand side of a 'for...in' statement must be of type 'string' or 'any'."console.log(k);}letk2: keyofFoo;for(k2inf){// No errorconsole.log(k2);}}
Expected behavior:
Something like "The index type of the right side of a 'for...in' statement must be assignable to the left side" perhaps, with details on the failure of assignability?
Actual behavior:
"The left-hand side of a 'for...in' statement must be of type 'string' or 'any'."
The text was updated successfully, but these errors were encountered:
keyof T is string, so why is this error message wrong?
I assume you meant keyof Foo. I don't think so: when I mouse over k2 in Visual Studio Code, it shows the type as "x" | "y". In any case, if I change the annotation to "x" | "y", there is no error, so the error message stating that the type must be string or any is definitely wrong.
Then how do you explain that let k: "x" is an error, but let k: "x" | "y" | Date is allowed? The rule as implemented appears to be that keyof T must be assignable to the type of the left side, and the diagnostic should reflect that.
TypeScript Version: 2.1.5
Now that the left side of a 'for...in' statement is apparently allowed to be
keyof
the right side, the diagnostic for when the left side has the wrong type needs to be updated to account for that possibility.(Aside: Is it intentional that the left side won't be inferred to have the
keyof
type because of the unsoundness described in #12314, but it is allowed to be explicitly declared as that type?)Code
Expected behavior:
Something like "The index type of the right side of a 'for...in' statement must be assignable to the left side" perhaps, with details on the failure of assignability?
Actual behavior:
"The left-hand side of a 'for...in' statement must be of type 'string' or 'any'."
The text was updated successfully, but these errors were encountered: