microsoft / microsoft/TypeScript
Unexpected widening of template literal string type when the function's type is referenced by the constraint of a additional uninvolved type parameter
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
"Generic constraints", "Infer strict return type"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Strict function types
⏯ Playground Link
No response
💻 Code
First example:
type Primitives = number | string | boolean | bigint | symbol | undefined | null | void;
declare function call<
R extends Primitives,
O extends { a: (...args: any[]) => R },
Args extends { a: Parameters<O['a']> }
>(o: O, args: Args): ReturnType<O['a']>;
const check = call(
{
a(a: string) {
return `${a}a`;
}
},
{ a: ['a'] }
)
Second example:
type Primitives = number | string | boolean | bigint | symbol | undefined | null | void;
declare function call<
R extends Primitives,
O extends { a: (...args: any[]) => R },
>(o: O, args: { a: Parameters<O['a']> }): ReturnType<O['a']>;
const check = call(
{
a(a: string) {
return `${a}a`;
}
},
{ a: ['a'] }
)
🙁 Actual behavior
I'm trying to write a function that retrieves the function's strict return type (which is a property of an object) and takes a second argument as an object with the same properties, but with arguments for these functions.
There is a problem with how TypeScript infer the return types.
In the first example check will have string type. But in the second example check will have ${string}a type as expected.
I have noticed that problem occur when I try to write generic for args argument of call function.
This looks like a bug, since generic for args argument has no effect on other types.
🙂 Expected behavior
In both examples the return type must be ${string}a.
Additional information about the issue
No response
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 two generic call declarations and checking the inferred type of check in each example. Investigate TypeScript's generic constraint and return-type inference paths, then add a regression test showing that the first example preserves the ${string}a template literal type just like the second.
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
- 42/100