1

I'm new to both React and TypeScript so there might be something obvious that I have missed. I'm trying to do the following:

const props = Object.assign({}, this.props, {
    loaded: this.state.loaded
});

I get this error in my .tsx file: Property 'assign' does not exist on type 'Object constructor'.

Found this thread and tried:

const props = (<any>Object).assign({}, this.props, {
    loaded: this.state.loaded
});

Gave me this error: Property 'any' does not exist on type 'JXS.IntrinsicElements'.

I also tried this which returned Unexpected modifier for declare.

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

What have I missed?

1
  • That's kinda weird. Can you give a little more context? E.g. the Component you're using it and also maybe your tsconfig? Because there should be no issue with you first code snippet. Commented Apr 24, 2017 at 14:33

1 Answer 1

3

Object.assign is a es6 feature so you need to target es6:

{
    "compilerOptions": {
        ...
        "target": "es6",
        ...
    }
}

If you can't target es6 but still know for sure that your code will run in environments that do support new es6 features then you can use the lib:

{
    "compilerOptions": {
        ...
        "target": "es5", // for example
        "lib": ["DOM", "ES6", "ScriptHost"],
        ...
    }
}

Another option is to add this funtionality yourself.
You'll need to add the definitions (copied from the lib.es6.d.ts):

interface ObjectConstructor {
    assign<T, U>(target: T, source: U): T & U;
    assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;
    assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
    assign(target: any, ...sources: any[]): any;
}

And then polyfill it.

As for the casting part that you tried, you can't use this form of casting inside tsx files, you need to do this:

const props = (Object as any).assign({}, this.props, {
    loaded: this.state.loaded
});

Because in tsx files the compiler thinks that <any> as an html/react element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.