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
Sometimes for quick debugging, I add an early return to skip a bunch of code. However, this frequently causes new TypeScript warnings to be shown. These are not valid warnings, but rather a bug.
🔎 Search Terms
return, possibly undefined
🕗 Version & Regression Information
Have seen this since I started using TS
⏯ Playground Link
N/A
💻 Code
export async function TEST() {
const obj: { notify?: { option1: boolean } } = {};
const result = { notify: true };
if (obj.notify) obj.notify.option1 = result.notify;
return false; //The return causes TS to parse subsequent lines differently.
if (obj.notify) obj.notify.option1 = result.notify; //Even though the if should address the concern, generates wavy red error: Object is possibly 'undefined'.ts(2532)
}
🙁 Actual behavior
The exact same line of code is treated differently if it happens to be unreachable (places after a return statement).
🙂 Expected behavior
Code should be treated the same, the return statement has no effect on possible pathways or variables that might create a valid concern.
Bug Report
Sometimes for quick debugging, I add an early return to skip a bunch of code. However, this frequently causes new TypeScript warnings to be shown. These are not valid warnings, but rather a bug.
🔎 Search Terms
return, possibly undefined
🕗 Version & Regression Information
Have seen this since I started using TS
⏯ Playground Link
N/A
💻 Code
🙁 Actual behavior
The exact same line of code is treated differently if it happens to be unreachable (places after a return statement).
🙂 Expected behavior
Code should be treated the same, the return statement has no effect on possible pathways or variables that might create a valid concern.
tslint.json:
{
"defaultSeverity": "error",
"extends": ["tslint:recommended"],
"linterOptions": {
"exclude": ["bin", "../Other/."]
},
"rules": {
"no-async-without-await": false,
"no-floating-promises": true,
"await-promise": true,
"no-return-await": false,
"no-promise-as-boolean": true,
"promise-function-async": true,
"no-angle-bracket-type-assertion": true,
"prefer-for-of": true,
"triple-equals": true,
"no-shadowed-variable": true,
"static-this": true,
"use-isnan": true,
"deprecation": true,
"class-name": true,
"encoding": true,
"trailing-comma": false,
"comment-format": { "check-space": false },
"no-console": false,
"prefer-const": false,
"no-var-requires": false,
"no-trailing-whitespace": false,
"only-arrow-functions": false,
"object-literal-shorthand": false,
"semicolon": false,
"no-bitwise": true,
"no-debugger": false
}
}
tsconfig.json:
{
"compilerOptions": {
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,
"strict": true,
"noImplicitReturns": true,
"resolveJsonModule": true,
"noImplicitAny": true,
"noImplicitThis": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist/",
"sourceMap": true,
"target": "es2018",
"module": "esnext",
"moduleResolution": "node",
"lib": ["DOM", "DOM.Iterable", "ES6"],
"esModuleInterop": true,
"assumeChangesOnlyAffectDirectDependencies": true,
"jsx": "react"
},
"exclude": ["node_modules"]
}
The text was updated successfully, but these errors were encountered: