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

Introduce StrictKeyOmit #2968

Closed
wants to merge 1 commit into from

Conversation

Andarist
Copy link

@Andarist Andarist commented Nov 9, 2024

fixes #2952

I couldn't run codegen script for some reason so I didn't regenerate the output yet. I end up with:

> @atproto/api@0.13.15 codegen /Code/atproto/packages/api
> node ./scripts/generate-code.mjs && lex gen-api --yes ./src/client ../../lexicons/com/atproto/*/* ../../lexicons/app/bsky/*/* ../../lexicons/chat/bsky/*/* ../../lexicons/tools/ozone/*/*

flex: can't open gen-api

This improves type safety by omitting only the desired literal keys when index signatures are present (leaving the index signatures in place!). You can see this in action in this TS playground

Copy link
Author

Choose a reason for hiding this comment

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

There might be a better place for this utility type. I don't know where you keep things like this so I just shoved it here for now

[K in keyof T as IsLiteralKey<K> extends 1 ? K : never]: K
}

export type StrictKeyOmit<T, K extends LiteralKey<T>> = {
Copy link
Author

Choose a reason for hiding this comment

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

K extends LiteralKey<T> is only needed for completions - that's something that you don't quite need because this is only used by a generated code. But the same filtering would have to be done elsewhere without this constraint so it feels nice to have it here

Comment on lines +192 to +196
type IsLiteralKey<K extends PropertyKey> = {
string: string extends K ? 0 : 1
number: number extends K ? 0 : 1
symbol: symbol extends K ? 0 : 1
}[K extends string ? 'string' : K extends number ? 'number' : 'symbol']
Copy link
Author

Choose a reason for hiding this comment

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

This isn't 100% complete. It might not handle index signatures with template literal types or string mappings (like Uppercase<string>) but I seriously doubt you care about any of those here.

In a sense, support for number and symbol is already beyond what you need here but it was easy enough to add it so I did it for the completeness' sake

@matthieusieben
Copy link
Contributor

Thanks. I included something like this in #2956

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.

Generation of Omit in lex-cli types causes loss of type safety
2 participants