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.

Comparing constrained generic types/substitution types to conditional types

See original GitHub issue

When comparing a generic type to a conditional type whose checkType is the same type, we have additional information that we are not utilizing… for instance:

function f<T extends number>(x: T) {
    var y: T extends number ? number : string;

    // `T` is not assignable to `T extends number ? number : string`
    y = x;
 }

Ignoring intersections, we should be able to take the true branch all the time based on the constraint.

The intuition here is that T in the example above is really T extends number ? T : never which is assignable to T extends number ? number : string.

Similarly, with substitution types, we have additional information that we can leverage, e.g.:

declare function isNumber(a: any): a is number;

function map<T extends number | string>(
    o: T,
    map: (value: T extends number ? number : string) => any
): any {
    if (isNumber(o)) {
        // `T & number` is not assignable to `T extends number ? number : string`
        return map(o);
    }
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:29
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
kevinbealcommented, May 1, 2018

@RyanCavanaugh I think I have one.

interface ControlResult<T> {
	fields: ConvertPrimitiveTo<T, FormControl>;
	controls: T extends any[] ? FormArray : FormGroup;
}

From #23803

I think @mhegazy 's alternative solution there may work though.

1reaction
MicahZoltucommented, Aug 19, 2019

@Gianthra Despite me being the one to refer you to this thread, I think I was mistaken originally and your problem is actually different. In your case, the problem lies in the fact that T could be a more constrained type than boolean. This will cause typeof default === 'boolean' to resolve to true, but T may be the type false.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Conditional Types - TypeScript
Just like with narrowing with type guards can give us a more specific type, the true branch of a conditional type will further...
Read more >
The guide to conditional types in TypeScript - LogRocket Blog
One of the main advantages of conditional types is their ability to narrow down the possible actual types of a generic type.
Read more >
How to describe constraint using conditional type which is ...
I have a problem with typings in my code - union type constraint is generated instead of picking only one type from ItemProperties...
Read more >
TOSCA Simple Profile in YAML Version 1.2 - OASIS Open
This can be done by using the generic dependency requirement which is defined by the TOSCA Root Node Type and thus gets inherited...
Read more >
C# in Depth: Puzzles, Gotchas, Questions at Interviews - MyWays
Generics and arrays including specializations, constraints on generics, ... how to compare them, how arithmetic overflow behaves with different types, ...
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