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
interface Message {
content?: string;
}
class MessageComponent {
message!: Message;
dummy(): number {
// N.B.: attempts to temporarily disable method-impl
if(true) {
// causes: error TS2532: Object is possibly 'undefined'.
// on line with indexOf, eventhough it clearly does
// a check for: !== undefined in the line before it
return -1;
}
//if(true as boolean) { // works fine
// return -1;
//}
//if(1===1) { // works fine
// return -1;
//}
// the original method-impl
if(this.message.content !== undefined) {
return this.message.content.indexOf('---');
}
return -1;
}
}
π Actual behavior
It throws an compile-time error that in the following (unreachable) code:
the variable this.message.content may be undefined, when attempting to call indexOf.
π Expected behavior
There probably should be merely a warning of unreachable code, not a blocking compile-time error about a variable potentially being undefined, when one can clearly see it cannot be undefined (whether or not the code was reachable)
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
π Search Terms
none
π Version & Regression Information
All tested versions of TypeScript (tested from 3.3.3 until 5.4.5 in the playground)
β― Playground Link
https://www.typescriptlang.org/play?target=9&jsx=0&module=7&ts=5.4.5#code/JYOwLgpgTgZghgYwgAgLIQM4bgcxQbwFgAoZZBAe3AnAH4AuZDMKUHAbhIF8SSEAbOFjSZseAMIUAtgAcqNMMiKlkU0bggBCRuiwbOxEmQAmAVylSAngAoAlIxDmARtCVGyyAPSfkAOQB0AEL+jHBgkLJgGMhgFDEQshRQcKz8lsjGwNhO-ChqYAAWFMYAtMCy-LwqZMAw1iymELZu1R7e5HCmGJiM0FBJyAAqAMoATACsAMyjjADyTgBWEAiKWchyWMA56QDkpiDGEDCgEMY7-u4eXj5XV1TI-CfIAO7AhcighwAeszAANMgIAA3BRFUw4AofRQCCApNIZCiYS5tG63MhwcgFZYAa2QMCSjE0AF4ich9odjiBTh8QDEsQ8ni58VAUG8SABIK4ssCmKC0koARgMVx4rWR3lq9SgjWQQmQTgoFFycBAzXw1xeSWx0UpEHFN25vP5Qv1oqu+slApJRIFao1zy1OpO+rIhr5yEFwpRovZLsKKCSwBwoDg-FUEEKxTKFWRksKWX8aj0eH8lGo4GQxNJ5KOJ2MauRXIjRrpCaTYggqaokHA-k+EB+dR2JRbO1sXrIZo8yLdxq9otFfCoGCVlf4FBw1h2cCcCHoO2QAGpkFTnsgACJhCB2fx4MCDcrb2y2IA
π» Code
π Actual behavior
It throws an compile-time error that in the following (unreachable) code:
the variable this.message.content may be undefined, when attempting to call indexOf.
π Expected behavior
There probably should be merely a warning of unreachable code, not a blocking compile-time error about a variable potentially being undefined, when one can clearly see it cannot be undefined (whether or not the code was reachable)
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: