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.

Conditional types documentation missing information crucial for their usage.

See original GitHub issue

Bug Report

Documentation for conditional types is missing very important information about limitations. If someone wishes to use them frustration ensues because it is not working as one would expect. Documentation uses throw “unimplemented”; and gives no warnings about issues you could run into when actually making an implementation.

After several hours of trying and searching and already strongly believing it is a bug, I found that it is Design limitation - https://github.com/microsoft/TypeScript/issues/22735

🔎 Search Terms

conditional types not working, TS2322, conditional types, broken conditional types

Problematic code


interface IdLabel {
  id: number /* some fields */;
}
interface NameLabel {
  name: string /* other fields */;
}
type NameOrId<T extends number | string> = T extends number
  ? IdLabel
  : NameLabel;
function createLabelBad<T extends number | string>(idOrName: T): NameOrId<T> {
  if (typeof idOrName === 'string') {
      return {name: idOrName}; // TS2322
  }
  return {id: idOrName}; // TS2322
}
function createLabelGood<T extends number | string>(idOrName: T): NameOrId<T> {
  if (typeof idOrName === 'string') {
      const nameLabel: NameLabel = {name: idOrName};
      return nameLabel as any;
  }
  const idLabel: IdLabel = {id: idOrName};
  return idLabel as any;
}

🙁 Actual behavior

Very important information is omitted from documentation. This causes unnecessary frustration and headaches and lost time.

🙂 Expected behavior

Very important information about limitations of conditional types is mentioned possibly with some workarounds presented instead of throw “unimplemented”;

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
mitsos1oscommented, Mar 29, 2022

I just came here to write the same thing and found this issue!

Since it is a known limitation / bug, the documentation should at least mention the recommended workaround to use and actually provide an implementation for the createLabel method instead of a simple error throwing

Just for reference except for @psznm 's answer where you type the return values of the function, you can also use the conditional typed function as an overload and write the function without any return type (inferred). This will also work. Specifically:

function createLabel<T extends number | string>(idOrName: T): NameOrId<T>
function createLabel<T extends number | string>(idOrName: T) {  
  if (typeof idOrName === 'number') {
    return { id: idOrName };
  } else {
    return { name: idOrName };
  }
}

TSPlayground link

1reaction
psznmcommented, Jan 27, 2022

See also #33912

Thank you! The workaround from that issue is the thing I would love to see in the documentation! With that information I am now able to make it much cleaner and safer.

function createLabel<T extends number | string>(idOrName: T): NameOrId<T> {
  if (typeof idOrName === 'string') {
      return {name: idOrName} as NameOrId<T>;
  }
  return {id: idOrName} as NameOrId<T>;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Relate control flow to conditional types in return types #33912
Developers are eager to use conditional types in functions, ... Conditional types documentation missing information crucial for their usage.
Read more >
Documentation - Conditional Types - TypeScript
Create types which act like if statements in the type system.
Read more >
TypeScript: Conditional Types Explained - Ross Bulat - Medium
Conditionals are often coupled with generic types (otherwise termed type parameters) to test whether such parameter meets a certain condition.
Read more >
Conditional formatting compatibility issues - Microsoft Support
One or more cells in this workbook contain a conditional formatting type that is not supported in earlier versions of Excel, such as...
Read more >
Dealing with missing data: Key assumptions and methods for ...
Appropriately dealing with missing can be challenging as it requires a careful examination of the data to identify the type and pattern 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