Closed
Description
Suggestion
Support multiple template literal string index signatures (initial support of which was added in TS 4.4 #44512):
const config: {
[a: `a.${string}`]: string[],
[b: `b.${string}`]: number[]
} = {
[`a.abc`]: ["a", "b"],
[`b.def`]: ["c", "d"]
}
🔍 Search Terms
template literal strings index signatures
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
📃 Motivating Example & Use Cases
Same use cases as single template literal index signature, but more useful.
One example are objects containing queries for databases/APIs that support it (Firebase Firestore...) for validation purposes.
For example, this is what an API to update Firestore documents which validates update paths could look like:
type BookDocument = {
[chapterText: `chapter.${string}.text`]: string,
[chapterAuthor: `chapter.${string}.authors.${string}`]: { name: string, email: string }
};
const {bookId, author, chapterId, chapterText} = req.params;
updateDoc<BookDocument>(
firestore.collection("books").doc(bookId),
{
[`chapters.${chapterId}.text`]: chapterText,
[`chapters.${chapterId}.authors.${author}`]: author
}
)
Current best workaround is to define an overloaded function with [path, value]
parameter pairs, but the downside is you can't supply a map or list of tuples (e.g. during class construction etc.).
Metadata
Metadata
Assignees
Labels
No labels