negating type constraints
See original GitHub issueSometimes it’s useful to put a limit on what a type parameter can be. In a way it is a counterpart of the extends
constraint.
Problem
Consider an example, a classic function that takes whatever and returns void:
function ignore<a>(value: a) : void {};
However we must not apply this function to Promises, because it might get us a temporal leak if we do.
function readFileAsync(): Promise<string>;
ignore(readFileAsync()); // <-- untracked promise, temporal leak
Unfortunately it is way too easy to get into a situation when a promise is passed to that function unintentionally as a result of refactoring:
// before
function readFileSync(): string;
ignore(readFileSync()); // typechecks, works as intended, no problem
// after refactoring
function readFileAsync(): Promise<string>; // <-- went async here
ignore(readFileAsync()); // typechecks, unintended temporal leak, big problem
Solution
The situation above could have been avoided if TypeScript allowed negating constraints:
function ignore<a unlike Promise<any>>(value: a): void {} // <-- hypothetical syntax
Issue Analytics
- State:
- Created 7 years ago
- Reactions:19
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Type negation in TypeScript
1. Generic argument constrained to be non array · 2. Empty string negation · 3. Generic constraints · 4. Generic constraints - 2...
Read more >Is it possibile to negate type parameter constraint in Haskell?
In Haskell, it's possible to add constraints to a type parameter. For example: foo :: Functor f => f a. The question: is...
Read more >Defining a negative type constraint : r/haskell - Reddit
I would like to define a type constrain that might be used like: wibble :: NoExceptionInstance e ... Defining a negative type constraint....
Read more >Generic constraint with not equals - Using Swift
It seems like it's possible to constraint a type to being another type. Then it should be possible to do the opposite. A...
Read more >Decidability of Systems of Set Constraints with Negative ...
systems of mixed positive and negative constraints, which are consid- ... Set constraints have numerous applications in program analysis and type.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Negating types could allow unions with a catch-all member, without overshadowing the types of the known members.
@vvscode phenomenal. I used this to build a static destructuring exhaustiveness check: