You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
Apologies for using the Issue: Other template, I genuinely do not know if this is a bug or not.
I'm currently investigating a TS compilation speed improvement in Zod (see relevant PR here)
It seems as though replacing usages of the builtin Pick and Partial with inlined, functionally equivalent types results in less type instantiations, which, when the delta is large, starts to affect typechecking performance.
Builtins:
// This utility is the same in both versions
type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];
// This uses native Pick and Partial
export type addQuestionMarks<
T extends object,
R extends keyof T = requiredKeys<T>
> = Pick<Required<T>, R> & Partial<T>;
Inlined:
// This utility is the same in both versions
type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];
// This uses inlined Pick and Partial
export type addQuestionMarks<
T extends object,
R extends keyof T = requiredKeys<T>
> = { [K in R]: Required<T>[K] } & {
[K in keyof T]?: T[K] | undefined;
};
In this minimal comparison, the difference is small but it's there. In the case of bigger projects using complex types via libraries like Zod, the difference seems to balloon substantially.
The main goal of this issue, I guess, is to find out the following:
Is this comparison fair, i.e. are the builtin and the inlined versions infact somehow different, and
If they are equivalent, why does the inlined version result in less instantiations?
System:
% npx envinfo
System:
OS: macOS 13.5.2
CPU: (10) arm64 Apple M1 Max
Binaries:
Node: 20.5.1 - ~/Library/Caches/fnm_multishells/79183_1696663768732/bin/node
npm: 9.8.0 - ~/Library/Caches/fnm_multishells/79183_1696663768732/bin/npm
% npm ls typescript
└── typescript@5.2.2
The text was updated successfully, but these errors were encountered:
The first question to ask is: does the inlined version behave the same when invoked on union types? My hunch is that the difference is down to Partial (and maybe Pick?) being distributive, i.e. Partial<A | B> is evaluated as Partial<A> | Partial<B>
More instantations is expected; you can think of an instantiation sort of like resolving a layer of indirection, and aliases are definitely a manifest level of indirection.
Acknowledgement
Comment
Apologies for using the
Issue: Other
template, I genuinely do not know if this is a bug or not.I'm currently investigating a TS compilation speed improvement in Zod (see relevant PR here)
It seems as though replacing usages of the builtin
Pick
andPartial
with inlined, functionally equivalent types results in less type instantiations, which, when the delta is large, starts to affect typechecking performance.Builtins:
Inlined:
Minimal reproduction repository
Compilation output in the minimal repro repo using native Pick and Partial:
Compilation output in the minimal repro repo using inlined Pick and Partial:
In this minimal comparison, the difference is small but it's there. In the case of bigger projects using complex types via libraries like Zod, the difference seems to balloon substantially.
The main goal of this issue, I guess, is to find out the following:
System:
The text was updated successfully, but these errors were encountered: