microsoft / microsoft/TypeScript
Support `!==` operator in `is` and `asserts` return type syntax
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔍 Search Terms
unknown, assert, narrow, undefined, "is not"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
This looks like something that would have been asked before, but it's hard to search for.
Right now asserts is a pretty magical syntax that lets you do:
function assert(proposition: unknown): asserts proposition {
if (!proposition) {
throw new Error()
}
}
function foo(value: unknown): {} | null {
assert(value !== undefined);
return value;
}
However, trying to express a more specialized function doesn't work
function assertNotUndefined<T>(value: T): asserts value is Exclude<T, undefined> {
if (value === undefined) {
throw new Error();
}
}
function foo(value: unknown): {} | null {
assertNotUndefined(value);
return value; // Doesn't work
}
This is because Exclude<unknown, undefined> is still just unknown, since unknown is not just an alias for {} | null | undefined. Assuming that won't change, how else can one express assertNotUndefined? Well, if we take the original assert function and try to retroactively rationalize its syntax as this
function assert(proposition: unknown): asserts proposition;
when called as
assert(proposition !== undefined)
(hand-waving) just specializes to this
function assert(proposition: unknown): asserts proposition !== undefined;
I don't care to bikeshed over whether the syntax should be asserts proposition is not undefined, whether the LHS or RHS operands can be flipped, whether != or other operators should exist, or if any type expressions other than primitive literals like undefined should be supported, so I'll leave that to the comments.
📃 Motivating Example
function assertNotUndefined(proposition: unknown): asserts proposition !== undefined;
function foo(value: unknown): {} | null {
assertNotUndefined(value);
return value; // Works!
}
💻 Use Cases
- What do you want to use this for?
A function that can excludeundefinedcan be composed with other functions, e.g.array.filter(isNotUndefined) - What shortcomings exist with current approaches?
There's no clean way to define such a function without ugly conditionals to handleunknown. - What workarounds are you using in the meantime?
Rewrite code in more verbose style, e.g. manually construct a new array using aforloop instead ofarray.filter.
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 motivating examples in the issue and compare the proposed !== assertion syntax with existing is and asserts return type syntax. The scope of supported operators and type expressions is intentionally unresolved, so done would require settling the syntax and semantics, implementing the type narrowing, and adding appropriate compiler coverage.
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
- 25/100