Skip to content
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

Provide a no-parameter creator when type is "empty" #15

Closed
wants to merge 1 commit into from

Conversation

tvald
Copy link
Contributor

@tvald tvald commented Jan 28, 2018

For "empty" types ({}, void, null), the Creator function no longer accepts an argument.

The spec for whether a type is empty is as follows:

// "T" (parameter required)
type T1 = ObjectHasKeys<1>
type T2 = ObjectHasKeys<''>
type T3 = ObjectHasKeys<true>
type T4 = ObjectHasKeys<symbol>
type T5 = ObjectHasKeys<{ foo: string }>
type T6 = ObjectHasKeys<{ foo?: string }>

// "F" (parameter not accepted)
type F1 = ObjectHasKeys<{}>
type F2 = ObjectHasKeys<null>
type F3 = ObjectHasKeys<void>

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling c2e24c0 on tvald-contrib:feature/nokey-types into f3cede5 on pelotom:master.

Copy link
Owner

@pelotom pelotom left a comment

Choose a reason for hiding this comment

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

This will be a great improvement 👍

{ [key: string]: { _: 'F' } }
)[L]['_']

export type ObjectHasKeys<O> = StringContains<keyof O, keyof O>
Copy link
Owner

Choose a reason for hiding this comment

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

How about just using typelevel-ts for this machinery? There's really no downside since it's purely a static dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually different from typelevel-ts's implementation of StringContains, which fails when applied to the keys of a primitive like number or boolean.

I can look into trying to work around that failure, or perhaps getting a PR into typelevel-ts, since it would definitely be preferable to depend on an external library for all of this.

Copy link
Owner

Choose a reason for hiding this comment

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

I see. Well, no need to go into contortions for it.

@pelotom
Copy link
Owner

pelotom commented Jan 29, 2018

Hm, maybe it would be better to only apply this trick when the union's value type is precisely undefined? Otherwise you can do this:

const { value } = unionize({ foo: ofType<null>() }, 'tag', 'value').foo()

The type system will infer value: null, but its value will actually be undefined... similar problem for ofType<{}>(). All we really need is one special nullish type which enables the no-arg creation function.

@pelotom
Copy link
Owner

pelotom commented Jan 29, 2018

Also could you add a test for this?

@tvald
Copy link
Contributor Author

tvald commented Feb 8, 2018

Closing this in favor of an implementation based on conditional types, since they'll provide far more flexibility for introducing type-dependent modifications of the unionize API.

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.

3 participants