Microsoft/TypeScript

ThisType is invalid when using object spread on generic object

Open

#52 207 ouverte le 12 janv. 2023

Voir sur GitHub
 (5 commentaires) (0 réactions) (0 assignés)TypeScript (6 726 forks)batch import
BugDomain: This-TypingHelp Wanted

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

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.

Guide contributeur