Typescript: Add Length Parameter to typed arrays

Created on 14 Sep 2017  路  6Comments  路  Source: microsoft/TypeScript

The goal is pretty straight forward, since typed arrays have a fixed length it would be a more accurate and helpful type it you could specify length. Then you could get complaints if you try to assign or access a higher index or use a array of the incorrect length.

Code

type Vector2d = Int32Array<2>;
type Vector3d = Int32Array<3>;
type Matrix = Int32Array<16>;

const add =
  (vecA:Vector2d) =>
    (vecB:Vector2d, target:Vector2d = new Int32Array(2) ):Vector2d => {
      target[0] = vecA[0] + vecB[0];
      target[1] = vecA[1] + vecB[1];
      return target;
    };

const add3d =
  (vecA:Vector3d) =>
    (vecB:Vector3d, target:Vector3d = new Int32Array(3) ):Vector3d => {
      target[0] = vecA[0] + vecB[0];
      target[1] = vecA[1] + vecB[1];
      target[2] = vecA[2] + vecB[2];
      return target;
    };

const position = new Int32Array(2);
const position3d = new Int32Array(3);
const velocity = new Int32Array([1,1]);
const velocity3d = new Int32Array([1,2,3]);

add(position)(velocity, position);
const newPosition = add(position)(velocity);

add3d(position3d)(velocity3d, position3d);
add(position3d)(velocity3d, position3d); // Fails due to incorrect array length
Awaiting More Feedback lib.d.ts Suggestion

Most helpful comment

A custom type guard (like @mhegazy's above) is definitely a good immediate solution, but I think it would be a huge improvement for TypeScript to specifically track the length of typed arrays.

For normal arrays, this would expand the scope of type-checking significantly, since array lengths can be modified after creation. However, typed arrays have fixed sizes at creation and can then be treated as a unique primitive.

Current Example

Here's an obvious bug which isn't currently caught at compile time:

function specialTransform (array: Uint8Array) {
    if (array.length !== 4) {
        throw new Error('This function is only for 4-byte Uint8Arrays.')
    }
    return new Uint8Array([array[0] + 1, array[2], array[3] - 1, array[4]])
}

const bytes = new Uint8Array([1, 2, 3]);

// Will always throw an error at runtime, but can't be identified at compile time:
specialTransform(bytes);

This code could never work, and the thrown error in specialTransform should never happen in production. It's only purpose is to inform the programmer during debugging that they are using specialTransform incorrectly.

This could be made harder-to-mess-up with a custom type guard:

interface SpecialType extends Uint8Array {
  readonly length: 4;
}

export function brandSpecialType(array: Uint8Array): SpecialType {
    if (array.length !== 4) {
        throw new Error('This function is only for 4-byte Uint8Arrays.')
    }
  return array as SpecialType;
}

function specialTransform (array: specialType) {
    return Uint8Array([array[0] + 1, array[2], array[3] - 1, array[4]])
}

// the user still needs to 
const bytes = Uint8Array([1, 2, 3]);

// Runtime error happens a little earlier, but still not at compile time:
const mySpecialType = brandSpecialType(bytes);

specialTransform(mySpecialType);

This is marginally better in some situations, but requires a lot more custom infrastructure. If I'm writing a library that operates on sets of specifically sized typed arrays (like a cryptographic library), I can't provide both flexibility and strict typing. (See bitcoin-ts's Secp256k1 for an example.)

(Please let me know if I'm missing a better solution here.)

I believe I can either allow the developer to pass my functions a simple Uint8Array (and type-check at runtime to make sure they did it right), or I can export infrastructure for "creating" SpecialTypes, which are just ways of tightening the type of the length parameter.

What I would like to see

IMO, it would be ideal if TypeScript had the ability to track the length of typed arrays, and check them using simple expressions. Something like:

function specialTransform (array: Uint8Array<length === 4>) {
    return new Uint8Array([array[0] + 1, array[2], array[3] - 1, array[4]])
}

// works
specialTransform(new Uint8Array([1, 2, 3, 4]));

// ts error: Argument of type 'Uint8Array<{length: 3}>' is not assignable to parameter of type 'Uint8Array<length === 4>'.
specialTransform(new Uint8Array([1, 2, 3]));

Along with allowing certain examples to be fully type-checked, this would allow significantly better interoperability between libraries which deal with typed arrays.


(Last note: I'm sure there's a more general feature that could be implemented and used to add this functionality to TypedArrays, but I'm not knowledgable enough to discuss the general case. I think it may be similar to the Tag types discussion, though since typed arrays are first-class JavaScript features, and for the other reasons above, I think TypeScript should have this kind of checking for typed arrays built-in.)

All 6 comments

you can already do this today with:

type Vector2d = MyInt32Array<2>;
type Vector3d = MyInt32Array<3>;
type Matrix = MyInt32Array<16>;

interface MyInt32Array<T extends number> extends Int32Array { 
    length: T;
}

interface Int32ArrayConstructor {
    new<T extends number>(length: T): MyInt32Array<T>;
}

I am not sure of the utility of pushing this change into the standard library though.

My only criticism of that suggestion is that it doesn't handle the accessing or setting of an index beyond the length.

Well that and having to create custom array types to mimic the existing data types.

My only criticism of that suggestion is that it doesn't handle the accessing or setting of an index beyond the length.

Array is not handled differently in the type system. so it will be strange if we start paying attention to length. on any rate we have similar requests in the context of tuples, see https://github.com/Microsoft/TypeScript/issues/6229.

A custom type guard (like @mhegazy's above) is definitely a good immediate solution, but I think it would be a huge improvement for TypeScript to specifically track the length of typed arrays.

For normal arrays, this would expand the scope of type-checking significantly, since array lengths can be modified after creation. However, typed arrays have fixed sizes at creation and can then be treated as a unique primitive.

Current Example

Here's an obvious bug which isn't currently caught at compile time:

function specialTransform (array: Uint8Array) {
    if (array.length !== 4) {
        throw new Error('This function is only for 4-byte Uint8Arrays.')
    }
    return new Uint8Array([array[0] + 1, array[2], array[3] - 1, array[4]])
}

const bytes = new Uint8Array([1, 2, 3]);

// Will always throw an error at runtime, but can't be identified at compile time:
specialTransform(bytes);

This code could never work, and the thrown error in specialTransform should never happen in production. It's only purpose is to inform the programmer during debugging that they are using specialTransform incorrectly.

This could be made harder-to-mess-up with a custom type guard:

interface SpecialType extends Uint8Array {
  readonly length: 4;
}

export function brandSpecialType(array: Uint8Array): SpecialType {
    if (array.length !== 4) {
        throw new Error('This function is only for 4-byte Uint8Arrays.')
    }
  return array as SpecialType;
}

function specialTransform (array: specialType) {
    return Uint8Array([array[0] + 1, array[2], array[3] - 1, array[4]])
}

// the user still needs to 
const bytes = Uint8Array([1, 2, 3]);

// Runtime error happens a little earlier, but still not at compile time:
const mySpecialType = brandSpecialType(bytes);

specialTransform(mySpecialType);

This is marginally better in some situations, but requires a lot more custom infrastructure. If I'm writing a library that operates on sets of specifically sized typed arrays (like a cryptographic library), I can't provide both flexibility and strict typing. (See bitcoin-ts's Secp256k1 for an example.)

(Please let me know if I'm missing a better solution here.)

I believe I can either allow the developer to pass my functions a simple Uint8Array (and type-check at runtime to make sure they did it right), or I can export infrastructure for "creating" SpecialTypes, which are just ways of tightening the type of the length parameter.

What I would like to see

IMO, it would be ideal if TypeScript had the ability to track the length of typed arrays, and check them using simple expressions. Something like:

function specialTransform (array: Uint8Array<length === 4>) {
    return new Uint8Array([array[0] + 1, array[2], array[3] - 1, array[4]])
}

// works
specialTransform(new Uint8Array([1, 2, 3, 4]));

// ts error: Argument of type 'Uint8Array<{length: 3}>' is not assignable to parameter of type 'Uint8Array<length === 4>'.
specialTransform(new Uint8Array([1, 2, 3]));

Along with allowing certain examples to be fully type-checked, this would allow significantly better interoperability between libraries which deal with typed arrays.


(Last note: I'm sure there's a more general feature that could be implemented and used to add this functionality to TypedArrays, but I'm not knowledgable enough to discuss the general case. I think it may be similar to the Tag types discussion, though since typed arrays are first-class JavaScript features, and for the other reasons above, I think TypeScript should have this kind of checking for typed arrays built-in.)

IMO, it would also be useful to be able to enforce non-zero array length.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

remojansen picture remojansen  路  3Comments

dlaberge picture dlaberge  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

siddjain picture siddjain  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments