microsoft / microsoft/TypeScript
Index signature alternative
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
- Index signature alternative
- Index signature
Suggestion
An alternative of Index signature would be beneficial when you want to enforce, that all properties of an object must follow given pattern, without the need of [key: string | number]: T1.
My suggestion is to enable the use of ? for index signatures to mark them as type guards only.
type posParams = {
[name?: string]: UrlSegment;
};
Use Cases
Whenever there is a need for an object with unknown keys, but known value types this would come in handy. A lot of people trying to implement some sort of callback that requires the index signature fails.The enormous number of results if one googles Index signature is missing in type confirms this
I personally tend to confront this problem regularly when working with frameworks..
Examples
For this example, I will use Angular, but this is applicable to any typescript code.
type UrlMatchResult = {
consumed: UrlSegment[];
posParams?: {
[name?: string]: UrlSegment;
};
};
When one would be to implement it with only specific posParams it would only check for the type of the object keys and values.
export interface IUrlParameters {
baseFolder: UrlSegment;
currentFolder?: UrlSegment;
}
export function folderMatcher(segments: UrlSegment[]): UrlMatchResult {
const posParams: IUrlParameters = {
currentFolder: segments[0],
};
return {
consumed: segments,
posParams,
}
}
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, etc.)
- [-] This feature would agree with the rest of TypeScript's Design Goals.
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
The issue proposes optional index-signature syntax for checking known property values, with examples using UrlSegment and Angular. No implementation files, entry points, or tests are named; start by locating TypeScript's index-signature type-checking and parser tests. Done means reaching a design decision and covering the accepted behavior with tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100