Property 'assign' does not exist in type 'ObjectConstructor'

I use TypeScript in my application, where I use the function:

Object.assign(this.success, success.json()) 

However, during compilation, I get the following error:

  error TS2339: Property 'assign' does not exist on type 'ObjectConstructor'. 

Do you have any ideas how I can get rid of this error?

+117
typescript
Mar 12 '16 at 15:11
source share
6 answers

You can use a statement type , for example:

 (<any>Object).assign(this.success, success.json()) 
+120
Mar 12 '16 at 15:42
source share

Setup:

If you use VS code (or if you see the tsconfig.json file):

You need to add the lib property to your tsconfig.json and then your editor will use the type definitions of the associated typing, and also give you intellisense.

Just add "lib": ["es2015", "es2017", "dom"] to your tsconfig.json and restart VS Code

 { "compilerOptions": { // ... "target": "es5", "lib": ["es2015", "es2017", "dom"] // ... } } 

See all tsconfig.json options here .

If you are using Visual Studio or MSBuild, include this tag:

 <TypeScriptLib>es2015, es2017, dom</TypeScriptLib> 

See all MSBuild typescript compiler options and their use here .




Check your work:

If you configured your project to use built-in types and restarted your editor, then your resulting type will look like this, and not a type like any when you use Object.assign :

code example 1




A note on polyfills and compatibility with older browsers

Note that if you upgrade to ES5 or lower and focus on IE11, you will need to enable polyfiles because the typewriter will not include polyfiles for you.

If you want to enable polyphiles (which follows), I would recommend using core-js polyphiles.

npm install --save core-js

Then at the entry point in your application (e.g. /src/index.ts ) add an import for core-js at the top of the file:

 import 'core-js'; 

If you are not using npm you can simply paste the next polyfill, taken from MDN, to some place in your code that is executed before using Object.assign .

 if (typeof Object.assign != 'function') { // Must be writable: true, enumerable: false, configurable: true Object.defineProperty(Object, "assign", { value: function assign(target, varArgs) { // .length of function is 2 'use strict'; if (target == null) { // TypeError if undefined or null throw new TypeError('Cannot convert undefined or null to object'); } var to = Object(target); for (var index = 1; index < arguments.length; index++) { var nextSource = arguments[index]; if (nextSource != null) { // Skip over if undefined or null for (var nextKey in nextSource) { // Avoid bugs when hasOwnProperty is shadowed if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { to[nextKey] = nextSource[nextKey]; } } } } return to; }, writable: true, configurable: true }); } 
+151
Nov 28 '16 at 7:27
source share

This is because you use the ECMAScript 6 function and configure ECMAScript 5 or 3. The easiest fix is ​​to set the correct target, for example, if you use Grunt:

 options: { target: 'es6' } 

change the corresponding properties tab in Visual Studio or manually by editing the .csproj file and find the TypeScriptTarget element and switch to ES6, for example:

 <TypeScriptTarget>ES6</TypeScriptTarget> 

If you need to target ES5 just add the following to your TypeScript code

 declare interface ObjectConstructor { assign(target: any, ...sources: any[]): any; } 

This combines an additional method, solving the problem. More details here . You may need a polyfill, depending on your browser compatibility requirements - for example, this MDN

 if (typeof Object.assign != 'function') { (function () { Object.assign = function (target) { 'use strict'; if (target === undefined || target === null) { throw new TypeError('Cannot convert undefined or null to object'); } var output = Object(target); for (var index = 1; index < arguments.length; index++) { var source = arguments[index]; if (source !== undefined && source !== null) { for (var nextKey in source) { if (source.hasOwnProperty(nextKey)) { output[nextKey] = source[nextKey]; } } } } return output; }; })(); } 
+122
Mar 30 '16 at 9:37
source share

I added typing:

 typings install dt~es6-shim --global --save 
+1
Nov 07 '16 at 11:40
source share

Why not use a distribution operator?

return {this.success,...success.json() || {}};

0
Feb 15 '18 at 1:14
source share

I know it was a long time, but it’s easy to fix here.

 (Object as any).assign(this.success, success.json()) 
-one
Jun 20 '19 at 15:47
source share



All Articles