Skip to content

Fix type error in ISpritesheetData#9896

Merged
bigtimebuddy merged 3 commits intopixijs:devfrom
Nearoo:dev
Dec 5, 2023
Merged

Fix type error in ISpritesheetData#9896
bigtimebuddy merged 3 commits intopixijs:devfrom
Nearoo:dev

Conversation

@Nearoo
Copy link
Copy Markdown

@Nearoo Nearoo commented Nov 24, 2023

Description of change

Removes the ? on the animations key of the ISpritesheetData type. This is because it results in the type Record<never, ...> for the Spritesheet class member animations here, making it impossible to use animations without type error in typescript.

The reason for this is that animation of ISpritesheetData is of type Record<keyof S['animations'], Texture[]>, where, due to the ?, keyof S['animations'] will evaluate to undefined & utils.Dict<string[]>, which is never.

Since Spritesheet.animations is always initialized (here), it shouldn't cause an issue. However, I don't have a broad overview over the codebase, someone might want to verify that.

Pre-Merge Checklist

I didn't add any tests because the error is on a type level, and the change is literally one character.

  • Tests and/or benchmarks are included
  • Documentation is changed or added
  • Lint process passed (npm run lint)
  • Tests passed (npm run test)

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented Nov 24, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6e4ab7d:

Sandbox Source
pixi.js-sandbox Configuration

@bigtimebuddy
Copy link
Copy Markdown
Member

What version of typescript? Do have a reproduction of the type error?

What about using | undefined instead? That animations field is optional and you're making it required, which is not correct.

@Nearoo
Copy link
Copy Markdown
Author

Nearoo commented Nov 27, 2023

  • typescript version: 5.3.2

Here's a reproducible example: https://gist.github.com/Nearoo/83c5190c075a12c3e2e8464d43eb5763

It will result in:

src/main.ts:32:15 - error TS7053: Element implicitly has an 'any' type because expression of type '"bar"' can't be used to index type 'Record<never, Texture<Resource>[]>'.
  Property 'bar' does not exist on type 'Record<never, Texture<Resource>[]>'.

32 const anims = s.animations['bar']
                 ~~~~~~~~~~~~~~~~~~~

I don't think | undefined would solve the issue, the type will still be never. The issue is that typescript takes the intersection of types for indexed access types, which wasn't always the case. The change was made here.

I understand your concern though. Maybe a better approach would be to change the type of the animations field in Spritesheet here to this?

animations: Record<keyof NonNullable<S['animations']>, Texture[]>;

@bigtimebuddy
Copy link
Copy Markdown
Member

Yes, the problem is on the Spritesheet class not the data interface.

@Nearoo
Copy link
Copy Markdown
Author

Nearoo commented Nov 28, 2023

Alright, update my branch. Would be cool if you could merge, we have a lot of //@ts-ignore over the codebase :)

@bigtimebuddy bigtimebuddy added this to the v7.3.2 milestone Nov 28, 2023
@bigtimebuddy bigtimebuddy modified the milestones: v7.3.2, v7.3.3 Nov 29, 2023
@bigtimebuddy bigtimebuddy added the ✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t label Dec 5, 2023
@bigtimebuddy bigtimebuddy merged commit 0238708 into pixijs:dev Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✅ Ready To Merge Helpful when issues are in the queue waiting to get merged. This means the PR is completed and has t

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants