microsoft / microsoft/TypeScript
flag to check for type-unreachable code
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
feature request
the allowUnreachableCode flag (when disabled) only checks for code that's syntactically unreachable. however, the check doesn't work at all when looking at types.
i propose an additional flag (as @RyanCavanaugh suggested keeping these two concerns separate, see https://github.com/microsoft/TypeScript/issues/45267#issuecomment-901297945) that checks for code that the type checker believes is unreachable. for example:
declare function foo(): never
const a = foo()
console.log(1) //no error
foo()
console.log(1) //error
assertNever
in order to allow the assertNever pattern, the flag could still allow unreachable code where an argument of type never is passed, but not code after never is returned. this way, the following code would still be valid:
declare function assertNever(value: never): never
declare const foo: 1|2|3
switch(foo) {
case 1:
break
case 2:
break
case 3:
break
default:
assertNever(foo)
}
🔎 Search Terms
unreachable code
⏯ Playground Link
Playground link with relevant code
💻 Code
if (false) console.log("asdf") //error
if (false as false) console.log("asdf") //no error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and reproduce both examples using the existing allowUnreachableCode behavior. Read the type-checking and unreachable-code diagnostics around these cases, then define the flag's behavior for never returns and assertNever calls. Done means the proposed type-based unreachable checks and exceptions are covered by agreed compiler tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100