Skip to content

Commit 1e3456c

Browse files
authored
fix: Make d.Infer<T> assignable to d.InferInput<T> (#2335)
1 parent a60872a commit 1e3456c

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

packages/typegpu/src/shared/repr.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export type Infer<T> = T extends { readonly [$repr]: infer TRepr } ? TRepr : T;
3838
* const arrayOfStructs = d.arrayOf(d.struct({ pos: d.vec3f, id: d.f32 }), 4);
3939
* type D = d.InferInput<typeof arrayOfStructs>; // { pos: d.v3f | Float32Array<ArrayBufferLike> | [number, number, number]; id: number; }[]
4040
*/
41-
export type InferInput<T> = T extends { readonly [$inRepr]: infer TRepr } ? TRepr : Infer<T>;
41+
export type InferInput<T> =
42+
| Infer<T>
43+
| (T extends { readonly [$inRepr]: infer TRepr } ? TRepr : never);
4244

4345
/**
4446
* Extracts a sparse/partial inferred representation of a resource.

packages/typegpu/tests/buffer.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { getName } from '../src/shared/meta.ts';
88
import type { IsValidBufferSchema, IsValidUniformSchema } from '../src/shared/repr.ts';
99
import type { TypedArray } from '../src/shared/utilityTypes.ts';
1010
import { it } from 'typegpu-testing-utility';
11-
import { buildWriter } from '../src/data/compiledIO.ts';
1211

1312
function toUint8Array(...arrays: Array<TypedArray>): Uint8Array {
1413
let totalByteLength = 0;
@@ -846,7 +845,7 @@ describe('TgpuBuffer (InferInput)', () => {
846845
expectTypeOf(arrBuf.write)
847846
.parameter(0)
848847
.toEqualTypeOf<
849-
(d.v3f | [number, number, number] | Float32Array)[] | Float32Array | ArrayBuffer
848+
(d.v3f | [number, number, number] | Float32Array)[] | Float32Array | ArrayBuffer | d.v3f[]
850849
>();
851850

852851
expectTypeOf(scalarArrBuf.write)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test } from 'typegpu-testing-utility';
2+
import { describe, expect } from 'vitest';
3+
import { d, ValidateBufferSchema } from 'typegpu';
4+
5+
describe('d.InferInput', () => {
6+
test('d.Infer<T> should be assignable to d.InferInput<T>', ({ root }) => {
7+
function foo<T extends d.AnyWgslData>(schema: ValidateBufferSchema<T>, input: d.Infer<T>) {
8+
return root.createBuffer(schema, input);
9+
}
10+
11+
expect(() => foo(d.f32, 1)).not.toThrow();
12+
});
13+
});

0 commit comments

Comments
 (0)