-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Bug Report
π Search Terms
type, inference, object, never
π Version & Regression Information
4.3.5
β― Playground Link
Playground link with relevant code
π» Code
interface UserInformationInput {
userId: string;
age: number;
}
function test(info: UserInformationInput) {
console.log("Before info Iteration:", info);
let k: keyof typeof info;
for (k in info) {
if (typeof info[k] === "string") {
info[k] = "mamamama"; // <-- Error!
// Type 'string' is not assignable to type 'never'.
}
else {
info[k] = 31; // <-- Error!
// Type 'number' is not assignable to type 'never'.
}
}
console.log("After info Iteration:", info);
}
test({
userId: "asdf",
age: 11
})
π Actual behavior
"typeof info[k]" is inferred to be "never"
But at the line 10(if typeof info[k] === "string), "typeof info[k]" was string or number.
Furthermore, after making sure the type of "info[k]" by using "if", it still remains to be "never".
And, although compiler throws an Error above, when I executed the code, it runs very well with the result below
[LOG]: "Before info Iteration:", { "userId": "asdf", "age": 11 }
[LOG]: "After info Iteration:", { "userId": "mamamama", "age": 31 }
π Expected behavior
I think "typeof info[k]" should be "string | number", not 'never'
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript