microsoft / microsoft/TypeScript
Feature Request: type for Arguments OF Function
Open
Nobody has claimed this yet.
In Discussion
Suggestion
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
Suggestion
current typescript support ...args
but when we use
declare function a(x: number, y: number) : number
declare function a(x: number, s: string) : string
a(...[1, 2]) // => Error: TS2556: Expected 2 arguments, but got 0 or more.
will get error
Use Cases
Examples
declare function a(x: number, y: number) : number
declare function a(x: number, s: string) : string
Actual behavior:
a(...[1, 2]) // => Error: TS2556: Expected 2 arguments, but got 0 or more.
type ArgumentsOFFunction<T extends Function>
a type can let typescript know this is args array of target function
notice: this type is not exists
Expected behavior:
let args1: ArgumentsOFFunction<typeof a> = [x = 1, s = ''];
let args2: ArgumentsOFFunction<typeof a> = [x = 1, y = 2];
let args3: ArgumentsOFFunction<typeof a> = ['1', 2]; // trigger a ts error => Error: TS2345: Argument of type '"1"' is not assignable to parameter of type 'number'.
a(...args1) // => string
a(...args2) // => number
a(1, 'str') // => string
a(1, 2) // => number
a('1', 2) // trigger a ts error => Error: TS2345: Argument of type '"1"' is not assignable to parameter of type 'number'.
declare function b(...args: ArgumentsOFFunction<typeof a>) : boolean
b(...args1) // => boolean
b(...args2) // => boolean
b(1, 'str') // => boolean
b(1, 2) // => boolean
b('1', 2) // trigger a ts error => Error: TS2345: Argument of type '"1"' is not assignable to parameter of type 'number'.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No file, test, or compiler entry point is named in the issue. Start by clarifying the intended behavior for overloaded functions, tuple arguments, and generic rest parameters, then add compiler tests covering the proposed ArgumentsOFFunction type, valid calls, and the shown type errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100