Negated types
See original GitHub issueSometimes it is desired to forbid certain types from being considered.
Example: JSON.stringify(function() {});
doesn’t make sense and the chance is high it’s written like this by mistake. With negated types we could eliminate a chance of it.
type Serializable = any ~ Function; // or simply ~ Function
declare interface JSON {
stringify(value: Serializable): string;
}
Another example
export NonIdentifierExpression = ts.Expression ~ ts.Identifier
Issue Analytics
- State:
- Created 8 years ago
- Reactions:43
- Comments:36 (5 by maintainers)
Top Results From Across the Web
Type negation in TypeScript
Type negation in TypeScript. #Type negation; #Negation of empty string; #Union constraints; #Generic constraints ...
Read more >Negating typescript types? - Stack Overflow
TypeScript doesn't have arbitrary negated types; Exclude<T, U> only filters unions in T . The type any is not a union so it...
Read more >Negated Types (this title is NOT a number) : r/typescript - Reddit
May I humbly suggest my tutorial on the never and unknown types? I tried to put in a balance of theory and practical...
Read more >negation in nLab
In usual type theory syntax negation is obtained as the function type into the empty type: ¬a=a→∅. 5. In categorical semantics. The ...
Read more >std::negation - cppreference.com
Forms the logical negation of the type trait B. The type std::negation<B> is a UnaryTypeTrait with a base characteristic of ...
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
I want this feature in some way or another, maybe even as a marker type like
ThisType
.I have a “language” for matching tree-like structures:
In this language of mine, I support
not
, which matches all types but its operand. Sonot string and not number
would disallow strings and numbers.I’ve got most of this excellently (over engineered to hell) typed:
The
match
parameter is as close as possible to what the original source expression describes. However, because TypeScript doesn’t have any way of representing the negation of types, I am forced to type anything that usesnot
asunknown
, which isn’t as great as I would’ve liked it to be.You can see the actual file here, on line 3, where I have no better type to give other than
unknown
😦If this was a thing in TypeScript, I would be able to provide even more accurate types in the callback of my library function.
Could this be re-opened in light of #29317?