microsoft / microsoft/TypeScript
Adding generic parameter to a lambda function removes typing from the function argument
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.2.2
Search Terms: generic removes parameter type
Code
// OKAY
// declare const labmda function with typings, then implement with minimal type annotations.
// "input" has type of "string".
export const foo: (input: string) => string
= (input) => (input);
// OKAY
// declare const lambda generic function without typings, then implement with full inline type annotations.
// "input" has type of "string".
export const bar
= <TResult>(input: string) => ({ } as Partial<TResult>);
// ERROR
// declare const labmda generic function with typings, then implement with minimal type annotations.
// "input" has type of "any".
export const baz: <TResult>(input: string) => Partial<TResult>
= <TResult>(input) => ({ [input]: input } as Partial<TResult>);
Expected behavior:
input is typed as string.
Actual behavior:
input is typed as any.
(NOTE: options -> noImplicitAny to have the issue highlighted as an error.)
Related Issues:
https://github.com/Microsoft/TypeScript/issues/18606
I think this issue might be related, but I don't understand it enough to judge.
If this is the same issue then the answer is "this is correct, and a limitation of the type system", which is fair enough.
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
Start by reproducing the example in the linked TypeScript Playground with noImplicitAny enabled, then compare the three lambda declarations and the related issue #18606. Trace how contextual typing is applied to the generic lambda assignment. Done means determining whether the argument should be inferred as string and adding a regression test if the behavior is confirmed as a bug.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100