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

Add SingleKey and IfEmptyObject #849

Merged
merged 8 commits into from
Mar 31, 2024

Conversation

ash-zzz
Copy link
Contributor

@ash-zzz ash-zzz commented Mar 30, 2024

Closes #813

I also added IfEmptyObject since that was a missing guard type that I needed to use for this

@sindresorhus
Copy link
Owner

I think it should be named SingleKeyObject. The current name is a bit ambiguous and could make you single it fetches a single key from an object, for example.

*/

// export type SingleKey<ObjectType> =
// IsUnion<keyof ObjectType> extends true ? never : {} extends ObjectType ? never : ObjectType;
Copy link
Owner

Choose a reason for hiding this comment

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

Leftover

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops I thought I removed them all

```

@category Object
*/
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
*/
*/

},
values: [1, 2]
});
// Compilation error
Copy link
Owner

Choose a reason for hiding this comment

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

Document the actual compilation here.

Comment on lines 20 to 22
operation: {
name: 'add'
},
Copy link
Owner

Choose a reason for hiding this comment

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

Keep the example simple by not using a nested property here.

@ash-zzz
Copy link
Contributor Author

ash-zzz commented Mar 30, 2024

I think it should be named SingleKeyObject. The current name is a bit ambiguous and could make you single it fetches a single key from an object, for example.

Agreed, I just renamed it and fixed the documentation

@sindresorhus sindresorhus merged commit fa1c3f3 into sindresorhus:main Mar 31, 2024
6 checks passed
@sindresorhus
Copy link
Owner

Thanks :)

@jakeboone02
Copy link

Thanks for doing this, @ash-zzz!

Should this work for types where the keys are unrestricted? If not, I'm not sure it satisfies my original request from #813, which was to declare an object type that requires exactly one key without restricting the key itself to any specific string or union of strings.

I tried SingleKeyObject<Record<string, any>> and SingleKeyObject<{ [k: string]: any }> but they both resolved to never. Something like SingleKeyObject<Record<'union' | 'of' | 'strings', any>> works fine.

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.

Require exactly one key of type string
3 participants