Skip to content

ThisType is invalid when using object spread on generic objectΒ #52207

Open
@paul-thebaud

Description

@paul-thebaud

Bug Report

πŸ”Ž Search Terms

ThisType Object Spread Generic

πŸ•— Version & Regression Information

  • I was unable to test this on prior versions because I was not using this feature previously

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function createObject<D extends {}>(def: D & ThisType<D>) { /* some code */ }

const namableObject = { name: '' };
// ThisType will be wrongly typed when using object spread
const spreadObject = createObject({
  ...namableObject,
  get thisName() {
    return this.name; // <-- Property 'name' does not exists on type '{}'
  },
});
// Correctly typed otherwise
const fullObject = createObject({
  name: '',
  get thisName() {
    return this.name;
  },
});

πŸ™ Actual behavior

The generic type is broken when the given object value contains a spread object.

πŸ™‚ Expected behavior

The generic type should contain the spreaded object properties as those are finally included into the result object.

Comment

This may be related to #26412 and other issues like this one but I didn't found a workarround to get a ThisType correctly typed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions