Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
paul-thebaud opened this issue Jan 12, 2023 · 5 comments
Open

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

paul-thebaud opened this issue Jan 12, 2023 · 5 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@paul-thebaud
Copy link

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.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Jan 12, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 12, 2023
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jan 12, 2023

I'm not immediately convinced this one isn't fixable-in-principle. The non-spread case works and there isn't a circularity, so I don't see any blockers to it working. I guess the only problem is that properties from namableObject might be overridden by properties later in the object literal?

@paul-thebaud
Copy link
Author

paul-thebaud commented Jan 12, 2023

Hi,
Thanks for your quick answers. I agree with @RyanCavanaugh, I dont see anything blocking here.
Yes, the namableObject could be overwritten, but the type of the currently constructing ThisType should still be inferrable IMO.

@paul-thebaud
Copy link
Author

@RyanCavanaugh, I don't know at all how Typescript works internally. I can make a PR for this if needed but I'll need some guidance.

@RyanCavanaugh
Copy link
Member

It's very unlikely the fix here is approachable by someone not already quite familiar with the TypeScript codebase. It's definitely hard enough that I can't offer any concrete guidance on where to start.

@paul-thebaud
Copy link
Author

OK no problem. I'll wait for any update on this and notify this issue in my package for now. Thanks a lot for those explanation and triage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants