Skip to content

Object spread and this: spread types may only be created from object types #12756

@sanex3339

Description

@sanex3339

TypeScript Version: 2.1.4

Code

example

interface IOptions {
    option1: string;
    option2: string;
    option3: string;
}

const DEFAULT_OPTIONS_PRESET: IOptions = {
    option1: 'string1',
    option2: 'string2',
    option3: 'string3'
};

class Options implements IOptions {
    public readonly option1: string;
    public readonly option2: string;
    public readonly option3: string;

    constructor (inputOptions: IOptions) {
        this = { // The left-hand side of an assignment expression must be a variable or a property access.
            ...this, // spread types may only be created from object types
            ...DEFAULT_OPTIONS_PRESET,
            ...inputOptions 
        };

        const errors = OptionsValidator.validate(this);

        if (errors.length) {  // ...  }

        this = { // The left-hand side of an assignment expression must be a variable or a property access.
            ...this,  // spread types may only be created from object types
            ...OptionsNormalizer.normalize(this)
        };
    }
}

Expected behavior:
No errors?
With object assign code works well

constructor (inputOptions: IOptions) {
    Object.assign(this, DEFAULT_OPTIONS_PRESET, inputOptions);
}

Actual behavior:
Two errors:
The left-hand side of an assignment expression must be a variable or a property access.
Spread types may only be created from object types.

If it's a a correct behaviour please explain me it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions