-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Suggestion
π Search Terms
type modifier, infer optional property
β 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.
β Suggestion
In the case of a type that is either T or T | undefined, typescript should be able to select the type when a type guard is applied instead of enforcing T.
π Motivating Example
A more complex scenario can be explored in the playground.
type SomeType = {
a: string;
b?: string;
c?: number;
}
type StringsOfSomeType = "a" | "b"; // typically this is dynamically defined
const obj: SomeType = { a: "foo" };
const props: Array<StringsOfSomeType> = ["a", "b"];
const p = props[1];
if (obj[p] === undefined) {
obj[p] = undefined; // I suggest this should be possible because the condition allow us to determine that we are in a case where the value type is `string | undefined`
}π» Use Cases
Applying transformations in middleware functions.
Currently I have found no workaround.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript