question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sometimes 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:open
  • Created 8 years ago
  • Reactions:43
  • Comments:36 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
kelsnycommented, May 28, 2022

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:

{
    type: "NOT_NUMBER";
    value: not number;
}

In this language of mine, I support not, which matches all types but its operand. So not string and not number would disallow strings and numbers.

I’ve got most of this excellently (over engineered to hell) typed:

fantasyLibrary.match(compiledExpression, myTree, (match) => {
    // match is of type { type: "NOT_NUMBER"; value: unknown; }
});

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 uses not as unknown, 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.

5reactions
dead-claudiacommented, Apr 22, 2019

Could this be re-opened in light of #29317?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found