Microsoft/TypeScript
Voir sur GitHubtype inference issue with dynamic template literals
Open
#58 402 ouverte le 2 mai 2024
Domain: check: Type InferenceHelp WantedPossible Improvement
Métriques du dépôt
- Stars
- (48 455 stars)
- Métriques de merge PR
- (Merge moyen 6j 17h) (9 PRs mergées en 30 j)
Description
🔎 Search Terms
"template literals and type inference", "type inference with dynamic template literals"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about it
⏯ Playground Link
💻 Code
type Api = {
[key: `hello.${string}`]: (data: { name: string }) => void
}
let userId: string = 'dynamic string'
// Example1: Doesn't work. type for `e` can't be inferred
const test1: Api = {
[`hello.${userId}`]: e => {
console.log(e.name)
},
}
// Example2: Works
const test2: Api = {
[`hello.static_string`]: e => {
console.log(e.name)
},
}
// Example3: Works
const on = <K extends keyof Api>(
key: K,
action: (data: Api[K]) => void,
) => {
// some logic
}
on(`hello.${userId}`, e => {
console.log(e.name)
})
🙁 Actual behavior
Example 1 doesn't work, while Example 2 and Example 3 works properly. The type for e can't be inferred
🙂 Expected behavior
To infer type of e in the Example 1.
Additional information about the issue
No response