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
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die beiden generischen Aufrufdeklarationen zu reproduzieren und den abgeleiteten Typ von check in jedem Beispiel zu überprüfen. Untersuche anschließend die Pfade für generische Constraints und die Rückgabetyp-Inferenz von TypeScript und füge dann einen Regressionstest hinzu, der zeigt, dass das erste Beispiel den Template-Literal-Typ ${string}a genau wie das zweite beibehält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100