DataArrayTexture not accepting Uint8Array (TypeScript)

I ran npm update on an older TypeScript project today. When I subsequently tried to compile the project, I was getting an error on a piece of code that was previously working:

const arrayTextureTestA = (data: Uint8Array) => {
	return new DataArrayTexture(data, 1024, 1024, 6)
}
ERROR in ./src/textures.ts:112:30
TS2345: Argument of type 'Uint8Array<ArrayBufferLike>' is not assignable to parameter of type 'BufferSource'.
  Type 'Uint8Array<ArrayBufferLike>' is not assignable to type 'ArrayBufferView<ArrayBuffer>'.
    Types of property 'buffer' are incompatible.
      Type 'ArrayBufferLike' is not assignable to type 'ArrayBuffer'.
        Type 'SharedArrayBuffer' is not assignable to type 'ArrayBuffer'.
          Types of property '[Symbol.toStringTag]' are incompatible.
            Type '"SharedArrayBuffer"' is not assignable to type '"ArrayBuffer"'.
    110 | }
    111 | const arrayTextureTestA = (data: Uint8Array) => {
  > 112 | 	return new DataArrayTexture(data, 1024, 1024, 6)
        | 	                            ^^^^
    113 | }

Why does DataArrayTexture not accept a Uint8Array anymore?

This makes the error go away:

const arrayTextureTestB = (data: Uint8Array) => {
	return new DataArrayTexture(new Uint8Array(data), 1024, 1024, 6)
}

// or like this
const arrayTextureTestC = (data: Uint8Array) => {
	return new DataArrayTexture(data as Uint8Array<ArrayBuffer>, 1024, 1024, 6)
}

This works, but I would like to understand the weirdness. Why is new Uint8Array(data) accepted but the plain data array is not?

I’m currently using three.js 0.180.0.

It sounds like you’ve updated to a newer TypeScript version and are running into the changes described at in Revert ts5.9 changes for Uint8Array · Issue #62240 · microsoft/TypeScript · GitHub . I don’t think there’s any three.js-specific change involved, you may need to cast the array explicitly with as Uint8Array<ArrayBuffer> to get rid of the error.

Or perhaps some change should be proposed in GitHub - three-types/three-ts-types: TypeScript types for the popular 3D library three.js , but I’m not sure.

1 Like