-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Suggestion] Override and identifier! for Mapped Type and extend interface #18567
Comments
can you give us some use cases of each of these requests? |
@mhegazy //A database doc type, for get
interface DocForGet {
id: number; //must have id on exist doc, auto generated by db.
updatedTime: string | null | undefined; //only have updatedTime if it have been updated (except first create)
field1: xxx;
...
fieldN: xxx;
} So for
With this request, you can achieve this by: type DocForInsert = {
[P in keyof DocForGet]: DocForGet[P]; // extend
id?: DocForInsert[id]; // override id to optional
updatedTime?: never; // can't set updatedTime on first create
}
type DocForUpdate = {
[P in keyof DocForGet]: DocForGet[P]; // extend
updatedTime: DocForGet[updatedTime]!; //override, identifier! would remove null and undefined
} |
The first portion can be defined by using intersection types, (try it on type DocForInsert = DocForGet & {
id?: DocForGet["id"]; // override id to optional
updatedTime: never; // force it to be never
}; the second part is tracked by #17948. Assuming that type Insert<T> = T & {
id?: number;
updatedTime: never;
};
type Get<T> = T & {
id: number;
updatedTime: string | null | undefined;
};
type Update<T> = T & {
id: number;
updatedTime: string;
}; |
Partion 1, tried on interface DocForGet {
id: number; //must have id on exist doc, auto generated by db.
updatedTime: string | null | undefined; //only have updatedTime if it have been updated (except first create)
field1: string;
}
type DocForInsert = DocForGet & {
id?: DocForGet["id"]; // override id to optional
updatedTime: never; // force it to be never
};
let test: DocForInsert = {
field1: 'test',
} Error:
|
You are correct, i was only thinking of the read part of it not the write. for that you need to use type Diff<T extends string, U extends string> = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T];
type Omit<T, K extends keyof T> = { [P in Diff<keyof T, K>]: T[P] };
type Overwrite<T, U> = { [P in Diff<keyof T, keyof U>]: T[P] } & U;
interface DocForGet {
id: number; //must have id on exist doc, auto generated by db.
updatedTime: string | null | undefined; //only have updatedTime if it have been updated (except first create)
field1: string;
}
type DocForInsert = Overwrite<DocForGet, { id?: DocForGet["id"]; updatedTime?: never }>;
let test: DocForInsert = {
field1: 'test',
}; |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
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:
Suggestion
null
andundefined
.Code
Benifit
BTW,
function getProperty
in #12578 is a solution, but it cannot be used in declaration files.(.d.ts
)The text was updated successfully, but these errors were encountered: