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.

negating type constraints

See original GitHub issue

Sometimes 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.


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


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:open
  • Created 7 years ago
  • Reactions:19
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

siegebellcommented, Nov 11, 2016

Negating types could allow unions with a catch-all member, without overshadowing the types of the known members.

interface A { type: "a", data: number }
interface B { type: "b", data: string }
interface Unknown { type: string unlike "a"|"b", data: any }
type ABU = A | B | Unknown

var x : ABU = {type: "a", data: 5}
if(x.type === "a") {
  let y =; // y should be inferred to be a number instead of any
AprilArcuscommented, Apr 3, 2020

@vvscode phenomenal. I used this to build a static destructuring exhaustiveness check:

interface ExtraProps {
  store: StoreType
  otherProp: OtherType
type Not<T> = { [key in keyof T]?: never }

function someHOC<OwnProps>(Component: React.FC<OwnProps>) {
  return ({ store, /* otherProp, */ ...ownProps }: ExtraProps & OwnProps) => (
    <Provider store={store}>
      <Component {...ownProps as Not<ExtraProps> /* error */ as OwnProps} />
Read more comments on GitHub >

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

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 Post

No results found

github_iconTop Related Hashnode Post

No results found