-
Notifications
You must be signed in to change notification settings - Fork 12.8k
rest parameter ts2556 and ts2345 #57170
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
Comments
Likely this particular error would get fixed with #50034 but it wouldn't fix the correlation problem. To fix the latter (and have it working today) you can use the pattern introduced by TS 4.6 (here): const BoxGeometry = (_width: number, _height: number) => undefined;
const SphereGeometry = (
_radius: number,
_height: number,
_openEnded?: boolean,
) => undefined;
type TypeMap = {
Box: Parameters<typeof BoxGeometry>;
Sphere: Parameters<typeof SphereGeometry>;
};
type GeometryType<P extends keyof TypeMap = keyof TypeMap> = {
[K in P]: (...args: TypeMap[K]) => undefined;
};
export const Geometry: GeometryType = {
Box: BoxGeometry,
Sphere: SphereGeometry,
};
export function load<K extends keyof TypeMap>(type: K, args: TypeMap[K]) {
const geometry = Geometry[type](...args);
} |
The example + links in #57170 (comment) is the best we can do without potentially combinatorial explosion |
So it seems that the change from #50034 (merged as part of #57122 ) didn't fix the "A spread argument must either have a tuple type or be passed to a rest parameter.(2556)" problem here. I think that part should still work, since TS is able to verify that const BoxGeometry = (_width: number, _height: number) => undefined;
const SphereGeometry = (
_radius: number,
_height: number,
_openEnded?: boolean,
) => undefined;
export const Geometry = {
Box: BoxGeometry,
Sphere: SphereGeometry,
};
type GeometryType = keyof typeof Geometry;
export type GetArgument<T extends GeometryType> = Parameters<
(typeof Geometry)[T]
>;
function acceptAtLeastOne(a: any, ...args: any[]) {}
export function load<T extends GeometryType>(type: T, args: GetArgument<T>) {
const geometry = Geometry[type](...args); // error (but a wrong one)
acceptAtLeastOne(...args); // error
const extendedArgs1 = [{}, ...args] as const; // ok
const extendedArgs2: readonly [any, ...any[]] = [{}, ...args] as const; // ok
} EDIT:// This might be a different long-standing issue~ since this comes from |
π Search Terms
ts2556 ts2345
π Version & Regression Information
version: 5.3.3
β― Playground Link
No response
π» Code
π Actual behavior
π Expected behavior
...args should not report an error
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: