I want to deconstruct an object and exclude a property.
This is what I'm doing right now:
 let x = {a:1, b:2, c:3};
 let y = {...x};
 delete y.c;
 store.setState(y);
I found an article about ES7 which states there is a new feature to exclude properties. So the above would be written like this:
 let x = {a:1, b:2, c:3};
 store.setState({c, ...x});
https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90
The above doesn't work in Angular 7 and I get the following error:
error TS2663: Cannot find name 'c'. Did you mean the instance member 'this.c'?
I'm currently running TypeScript 3.1.6, and my tsconfig.app.json file looks like this.
{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "module": "es2015",
        "types": []
    },
    "exclude": [
        "src/test.ts",
        "**/*.spec.ts"
    ]
}
Here is the parent file.
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "importHelpers": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}
Now I think the issue is that the line "lib": ["es2017"] is for ES6.
What can I do to enable this ES7 feature and will it break targeting ES5 as the compiled output from TypeScript?
Simply update your line - let y = {c, ...x}; with const {c, ...noC} = x;. So your code would be -
const x = {a:1, b:2, c:3};
const {c, ...noC} = x;
console.log(noC);Suggestion: It's a good practice to make x constant, if you are not going to update it later.
Order sholud be like this
let { c, ...y} = x;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With