-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
perf: speed up addQuestionMarks
#2620
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
[k in keyof T]: undefined extends T[k] ? never : k; | ||
}[keyof T]; | ||
type keepRequiredKeys<T extends object> = { | ||
[k in keyof T as undefined extends T[k] ? never : k]-?: T[k]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if some semantic difference is found using this approach then we could also try this
[k in keyof T as undefined extends T[k] ? never : k]-?: T[k]; | |
[k in keyof T as undefined extends T[k] ? never : k]-?: unknown; |
@Andarist Thanks for this! I only recently added in some decent tests for TLDR: This is a very clever approach but I couldn't get it to play nice with the current set of tests (mostly added a few weeks ago) in test("assignability", () => {
const createSchemaAndParse = <K extends string, VS extends z.ZodString>(
key: K,
valueSchema: VS,
data: unknown
) => {
const schema = z.object({
[key]: valueSchema,
});
const parsed = schema.parse(data);
const inferred: z.infer<z.ZodObject<{ [k in K]: VS }>> = parsed;
return inferred;
};
createSchemaAndParse("foo", z.string(), { foo: "" });
}); It seems the main issue with your For reference here's the current implementation: type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];
type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];
type pickRequired<T extends object, R extends keyof T = requiredKeys<T>> = {
[k in R]: T[k];
};
type pickOptional<T extends object, O extends keyof T = optionalKeys<T>> = {
[k in O]?: T[k];
};
export type addQuestionMarks<T extends object> = pickRequired<T> &
pickOptional<T> & { [k in keyof T]?: unknown }; |
0483a8e
to
d5c286c
Compare
It should understand this but:
I'm interested in learning more about this but I also don't have time to spend on this... what to do, what to do 😭 |
Just an experiment to speed up
addQuestionMarks
. I wonder if the change is 100% semantically the same and what your CI might tell us about it.