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.

syntax to control over distributivness

See original GitHub issue

from: https://github.com/Microsoft/TypeScript/issues/30569#issuecomment-476007534

so basically we need a way to say it clear and loud in the language whether we want:

Promise<A> | Promise<B>

or

Promise<A | B>

as a result of a type operation

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Jamesernatorcommented, Apr 8, 2022

(Unexpected) Distributivity of conditional types is something that bit me fairly strongly today, the fact some features are just magically distributive seems fairly confusing design in my opinion.

Like the main reason distributivity is wanted is so that people can specify things like Boxed<a | b> === Box<a> | Box<b>, but that seems more of a failure of the language to provide a way to specify a list of types to be distributed later.

Consider arrays, Array<A | B> is not Array<A> | Array<B>, the fact some generics just magically distribute seems more of an confusing feature than a predictable one. It’d be more obvious if there was just a way to declare a distributed union for any generic, i.e. maybe Array<A || B> === Array<A> | Array<B>. People then wouldn’t even need to define the wrappers, they could just || wherever they want spreading over a type constructor.

1reaction
jack-williamscommented, Apr 5, 2019

Tuples do have special treatment in that they correctly create substitution types in the true branch, effectively doing type narrowing—an object type will not do that. This is why the tuple method is proposed as the canonical way of doing this.

type OnlyNumber<N extends number> = [N];
type NoDistribution<T> = { o: T } extends { o: number } ? OnlyNumber<T> : "N" // error
type NoDistribution2<T> = [T] extends [number] ? OnlyNumber<T> : "N" // no error

I would also be reluctant to assign any special meaning to () in conditionals. () can already be used in types for grouping and they do not add any meaning they just change precedence (as they do in most other context): type A = (readonly number[])[].

Very much agree.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible regression in 3.4 with distributive conditional types
Using the distributive behavior of conditional types, UnionKeys gets the union of keys in each member of the union and the result is...
Read more >
The distribution syntax for random values—ArcMap
The following describe the syntax for the different available distributions for the various random tools: UNIFORM {Minimum}, {Maximum}—A uniform distribution ...
Read more >
Distribution keys - IBM® Netezza
You can use the following Netezza SQL command syntax to create tables and specify distribution keys: To create an explicit distribution key, the...
Read more >
The Normal Distribution and Control Charts
You are the manager at a new toy store and want to determine how many Monopoly games to stock in you store. The...
Read more >
User controlled unnormalized (propto) distribution syntax ...
The sampling statement form (with ~ ) drops normalizing constants in the samplers and optimizers. Based on the poll done in the linked...
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