Skip to content

Unable to create a function wrapperΒ #54569

@stronny

Description

@stronny

Bug Report

πŸ”Ž Search Terms

2556, tuple, union

πŸ•— Version & Regression Information

n/a

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

	class Test {
		one(s: string): void {s}
		two(n: number): boolean {return n > 100}
		ten(k: CryptoKey, w: Window): Document {k; return w.document}
	}
	const test = new Test()
	export function run<Name extends keyof Test, Args extends Parameters<Test[Name]>>(name: Name, ...args: Args): void {
		const fn = test[name]
		fn(...args)
	}

	run('one', '')
	run('two', 123)
	run('ten', null, window)

πŸ™ Actual behavior

TypeScript shows an error about the spread argument.

I've seen a bunch of similar issues, however I left unsatisfied with them. My specific concerns are these:

  1. Okay, so how do I create a wrapper function that preserves the static analysis?
  2. apply doesn't work as well, with a different error.
  3. Despite the error, TS seems to actually understand me and check the arguments of the wrapped functions, in particular:
  • I can't call run('five')
  • I can't call run('one')
  • I can't call run('one', 123)
  • I can't call run('ten', null, window) as demonstrated in the last line
  1. TS doesn't provide any facilities to ignore this particular error that I know is irrelevant to me.

I'm left with no options, and I think this deserves an issue.

πŸ™‚ Expected behavior

TypeScript doesn't show an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions