Skip to content

False warnings for code when unreachable code #42092

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

Closed
gregbenz opened this issue Dec 23, 2020 · 2 comments
Closed

False warnings for code when unreachable code #42092

gregbenz opened this issue Dec 23, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@gregbenz
Copy link

gregbenz commented Dec 23, 2020

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

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.

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"]
}

@RyanCavanaugh
Copy link
Member

Duplicate #26914

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 23, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants