Skip to content
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

Type for useTexture #219

Closed
SiegeSailor opened this issue Jan 8, 2021 · 3 comments · Fixed by #223
Closed

Type for useTexture #219

SiegeSailor opened this issue Jan 8, 2021 · 3 comments · Fixed by #223
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@SiegeSailor
Copy link

SiegeSailor commented Jan 8, 2021

The following snippet is from @react-three/drei#useTexture.

const texture = useTexture(url)

const [texture1, texture2] = useTexture([texture1, texture2])

According to the NPM document, useTexture accepts two kinds of input, which is string | string[]. However, on version 2.2.15, the type for useTexture is useTexture(url: string): Texture | Texture[], which is causing a constraint of inputting string[] and outputting only Texture or Texture[].

For example, the following code is reporting a type error.

const texture = useTexture('/asset/stacy.jpg');

return (
    <group>
        <primitive obejct={object}
        <skinnedMesh {...props}>
            <meshStandardMaterial map={texture} />
            // texture doesn't match `map` property because it only accepts `Texture`, and the type for texture is `Texture | Texture[]`.
        </skinnedMesh>
    </group>
       )

In conclusion, useTexture should be generic, output Texture when the input is string, and output Texture[] when the input is string[].

Package Version

  • typescript: 4.1.3
  • three: 0.124.0
  • @react-three/drei: 2.2.15
  • react-three-fiber: 5.3.11
@gsimone
Copy link
Member

gsimone commented Jan 8, 2021

Could you make a PR?

@Code47X
Copy link
Contributor

Code47X commented Jan 9, 2021

I also ran into this problem #176. I did a little digging and I think the current typing for url argument is always evaluating to a non array value. There is probably a more elegant TS solution out there, but this fixed it for me locally:

Current: (string type would never extend any[])
useTexture(url: string extends any[] ? string[] : string): Texture | Texture[];

Fixed for me:
useTexture<T extends string | string[]>(url: T): Texture | Texture[];

@joshuaellis
Copy link
Member

duplicate of #176 which now has a moving discussion, personally, i'd track it there.

@joshuaellis joshuaellis added bug Something isn't working duplicate This issue or pull request already exists labels Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants