-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
Description
TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)
2.5.2
Situation
There are lots of demand of more flexibility to Mapped Type. For example:
- Extend from a type and override some fields
- Turn optional field to required
- Only make some specific fields to optional
Suggestion
- Support multiple field definition in one type, the latter definition (same fields) will overwrite the previous
- Support identifier! for type definition which will remove
nullandundefined.
Code
type Full = {
field1: string;
field2: string;
....
fieldN: string;
}
//Suggestion 1: Multiple fields in type definition and support override
type SomeOptional = {
[P in keyof Full]: Full[P]; // extends
[P in 'field1' | 'field2']? : Full[P]; // override field1 and field2 to optional
xxx: string; // and can add more fields.
}
//Suggestion 2: Identifer! to remove null and undefined
type SomeRequired = {
[P in keyof SomeOptional]: SomeOptional[P]; // extends
field1: SomeOptional[field1]!; // ! will remove null and undefined from a type
}Benifit
- It can resolve most problem with required and optional in current open issues. (like Add a new type Required #15012, Type to invert optionality of properties #16173, Mapped type optional -> required property issue #13224, Is this possible? Opposite of Partial Type #12578, )
- More flexible to extend and override, in limited complexity.
BTW, function getProperty in #12578 is a solution, but it cannot be used in declaration files.(.d.ts)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code