这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: allow T.UnionOf to work with read only arrays #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

feat: allow T.UnionOf to work with read only arrays #48

wants to merge 2 commits into from

Conversation

pengx17
Copy link

@pengx17 pengx17 commented Sep 17, 2019

🎁 Pull Request

  • Used a clear / meaningful title for this pull request
  • Tested the changes in your own code (on your projects)
  • Added / Edited tests to reflect changes (tst folder)
  • Have read the Contributing part of the Readme
  • Passed npm test

Fixes

Why have you made changes?

  1. It is common to use as const to declare a tuple like the following:
const Foos = ['a', 'b'] as const;
type Foo = T.UnionOf<typeof Foos> // previously this will be an error
  1. O.UnNest
    Some times I want to get all possible types of the values of an object (used as a dict):
type Country = 'US' | 'UK' | 'CN'
type CountryCityMap = { US: ['NY', 'LA'], UK: ['London', 'Nottingham'], CN: ['Beijing', 'Shanghai'] };

// or O.UnNest<CountryCityMap>
// City = "NY" | "LA" | "London" | "Nottingham" | "Beijing" | "Shanghai"
type City = O.UnNest<CountryCityMap, Country> 

What changes have you made?

  • changed this to achieve this
  • changed that to achieve this
  • ...

What tests have you updated?

  • tested this in tst/...
  • tested that in tst/...
  • ...

Is there any breaking changes?

  • Yes, I changed the public API & documented it
  • Yes, I changed existing tests
  • No, I added to the public API & documented it
  • No, I added to the existing tests
  • I don't know

Anything else worth mentioning?

@pengx17 pengx17 requested a review from millsp as a code owner September 17, 2019 13:06
* ```
*/
export type UnNest<O, K extends Index = keyof O> = O extends {
[key in K]?: infer B;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, did not know this was possible

@millsp
Copy link
Owner

millsp commented Sep 17, 2019

Hey @pengx17 thanks for your pull request! I'm not going to accept it, but I'm going to help you out:

  • To achieve what your type does, use Path instead:
type Country = 'US' | 'UK' | 'CN'
type CountryCityMap = { US: ['NY', 'LA'], UK: ['London', 'Nottingham'], CN: ['Beijing', 'Shanghai'] };

// City = "NY" | "LA" | "London" | "Nottingham" | "Beijing" | "Shanghai"
type City = Path<CountryCityMap, [Country, any]>
  • And I've just pushed a new version that accepts readonly arrays for all the types, so you don't have to worry about this anymore :)

(cc @tonivj5)

@millsp millsp closed this Sep 17, 2019
@pengx17
Copy link
Author

pengx17 commented Sep 18, 2019

Thanks @pirix-gh. I saw you refactored all tuple types which is a great effort! Your repo on ts really inspires me a lot and thanks for maintaining it :D

@millsp
Copy link
Owner

millsp commented Sep 18, 2019

You're welcome @pengx17, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants