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.

Allowing `infer` after `extends T` in conditional types would simplify complicated type definitions massively

See original GitHub issue

Suggestion

🔍 Search Terms

  • distributed conditional types
  • infer keyword

✅ Viability Checklist

My suggestion meets these guidelines:

  • 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 TypeScript’s Design Goals.

⭐ Suggestion

Doing this would allow the complete removal of an entire branch, and it’s much more intuitive (the hanging infer stumped me a whole lot when I was learning).

Before:

InferringType<T> = T extends infer U ? U extends SomeConcreteType ? U : never : never

After:

InferringType<T> = T extends SomeConcreteType infer U ? U : never

Alternatives

  1. Something consistent with the as keyword in template string literal types e.g.
InferringType<T> = T extends SomeConcreteType as U ? U : never
  1. No extra syntax, just smart control flow analysis on the type variable.
InferringType<T> = T extends SomeConcreteType
  ? T // T extends SomeConcreteType on this branch
  : never // T does not extend SomeConcreteType on this branch

📃 Motivating Example

This playground link - has some documentation that should provide some context for the following code, but this is a pretty universal pattern anyway.

Before:

type InferEventFromTypeMatch<TypeMatch extends string> = TypeMatch extends infer InferredTypeMatch
  ? InferredTypeMatch extends Event['type']
    ? Extract<Event, AbstractEvent<InferredTypeMatch, any>>
    : InferredTypeMatch extends `${infer WildcardedTypeMatch}*`
    ? Event extends infer EventCase
      ? EventCase extends { type: `${WildcardedTypeMatch}${any}` }
        ? EventCase
        : never
      : never
    : never
  : never

After:

type InferEventFromTypeMatch0<TypeMatchString extends string> =
  TypeMatchString extends Event['type'] infer ExactTypeMatchString // Can give a much more meaningful label
    ? Extract<Event, AbstractEvent<ExactTypeMatchString, any>>
    : InferredTypeMatch extends `${infer WildcardedTypeMatch}*`
      ? Event extends { type: `${WildcardedTypeMatch}${any}` } infer WildcardTypeMatch // Can give a much more meaningful label here too
        ? WildcardTypeMatch 
        : never
      : never // Only two hanging nevers, rather than four

💻 Use Cases

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
yudai-nktcommented, Feb 4, 2022

Yes, but M doesn’t need to be covered by DottedKey for my specific use case. So I don’t really care at the moment.

1reaction
webstrandcommented, Jan 6, 2022

I think what you’re looking for already exists, with a slightly different syntax:

type Is<T extends U, U> = T

Translating your second version directly:

type Is<T extends U, U> = T;

type InferEventFromTypeMatch<TypeMatchString extends string> =
  TypeMatchString extends Is<infer ExactTypeMatchString, Event['type']>  // Can give a much more meaningful label
    ? Extract<Event, AbstractEvent<ExactTypeMatchString, any>>
    : TypeMatchString extends `${infer WildcardedTypeMatch}*`
      ? Event extends Is<infer WildcardTypeMatch, { type: `${WildcardedTypeMatch}${any}` }>  // Can give a much more meaningful label here too
        ? WildcardTypeMatch 
        : never
      : never // Only two hanging nevers, rather than four

But in your specific example you don’t need it at all:

type InferEventFromTypeMatch<TypeMatchString extends string> =
TypeMatchString extends Event['type']  // Can give a much more meaningful label
  ? Extract<Event, AbstractEvent<TypeMatchString, any>>
  : TypeMatchString extends `${infer WildcardedTypeMatch}*`
    ? Event extends { type: `${WildcardedTypeMatch}${any}` }  // Can give a much more meaningful label here too
      ? Event 
      : never
    : never // Only two hanging nevers, rather than four

There’s a cost to inference, so in idiomatic code we do not use it to rename types. Narrowing a type with T extends Something and then using T is not hard to follow, once you get used to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced TypeScript: The Power and Limitations of ... - Medium
The infer keyword can go inside of a conditional type to the right of the extends keyword. Using infer is asking TypeScript to...
Read more >
Documentation - Conditional Types - TypeScript
Create types which act like if statements in the type system. ... We can then use that conditional type to simplify our overloads...
Read more >
Understanding infer in TypeScript - LogRocket Blog
The infer keyword and conditional typing in TypeScript allows us to take a type and isolate any piece of it for later use....
Read more >
Understanding null safety - Dart
Since non-nullable types are modeled as subtypes of nullable types, implicit downcasts would let you pass a String? to something expecting a String...
Read more >
The Logic of Conditionals - Stanford Encyclopedia of Philosophy
Logics of conditionals deal with inferences involving sentences of the form ... Our goal in particular will be to allow readers to orient ......
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