-4

In another question I learned that for an interface

interface Example {
  a: 'string';
  b: 'number';
}

the identity

Example[a | b] = Example[a] | Example[b]

holds true in the typescript ecosystem, which is kind of surprising, since it seems not to be mentioned in any docs (at least I can not find it anywhere and would be happy if someone would actually show me some documentation, where it is mentioned).

If it is really not mentioned anywhere, can anyone tell me, why there exist unmentioned rules in typescript? Isn't that kind of sloppy? And far more interesting: What are other unspoken rules of typescript? Where can I get to know them?

1
  • Depends what you mean by unmentioned. Some things, are just a consequence of other rules. Some things are documented on github. The official docs are a starting point but do not cover advanced topics in great detail (maybe the next iteration of the docs which are in the works) Commented Jun 18, 2019 at 22:23

2 Answers 2

1

The documentation is not comprehensive. If it were, it would have been many times longer, and no one would bother reading it.

But in TypeScript repo, pull requests that add some feature usually have that feature pretty well documented. Unfortunately no one is doing the task of maintaining the reference manual that has everything in one place, so you have to do some digging.

For this rule

Example['a' | 'b'] = Example['a'] | Example['b']

you have to find out the release where keyof and indexed access types were introduced - it's on the roadmap page.

There you see a list of major pull requests and issues fixed for that release, one of them is

In that PR, you can find the rules, one of which says

Otherwise, when K is not a type parameter, T[K] is resolved as follows:

  • If K is a union type K1 | K2 | ... | Kn, T[K] is equivalent to T[K1] | T[K2] | ... | T[Kn]
Sign up to request clarification or add additional context in comments.

Comments

1

My suggestion would be hang out in the Typescript gitter https://gitter.im/Microsoft/TypeScript Alot of common problems/solutions/typescript theory get posted there, the documentation as mentioned above isn't comprehensive on purpose.

I would say sitting in there will help you learn extremely fast from other peoples problems and the solutions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.