I have a node.js project where I successfully use a custom mapped type
export type Mutable<T> = {
-readonly [P in keyof T]: T[P];
};
but if I add the same code to an Angular 6 project, compilation fails saying:
ERROR in src/assets/scripts/utils.ts(2,5): error TS1131: Property or signature expected.
src/assets/scripts/utils.ts(2,27): error TS1005: ']' expected.
src/assets/scripts/utils.ts(2,28): error TS1005: ';' expected.
src/assets/scripts/utils.ts(14,29): error TS1128: Declaration or statement expected.
src/assets/scripts/utils.ts(3,1): error TS1128: Declaration or statement expected.
Why does this happen and how can I solve?
Thank you all in advance!!
You are using an older version of typescript (2.7 or below). The ability to remove the readonly
modifier explicitly was only added in 2.8 (see PR and Roadmap).
You can use a trick to achieve the remove readonly result in prior versions of typescript as described here
type MutableHelper<T, TNames extends string> = { [P in TNames]: (T & { [name: string]: never })[P]};
type Mutable<T> = MutableHelper<T, keyof T>;