Skip to content

Commit

Permalink
fix(types): isTypedArray now performs type narrowing (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen authored Jul 12, 2024
1 parent d9cb58f commit 6b1f058
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// luma.gl, MIT license

// types
export type {TypedArray, NumericArray} from '@math.gl/types';
export type {TypedArray, TypedArrayConstructor, NumberArray, NumericArray} from '@math.gl/types';

export type {isTypedArray, isNumericArray} from '@math.gl/types';

Expand Down
8 changes: 4 additions & 4 deletions modules/types/src/is-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import {TypedArray, NumericArray} from './array-types';
* @param value value to be tested
* @returns input as TypedArray, or null
*/
export function isTypedArray(value: unknown): TypedArray | null {
return ArrayBuffer.isView(value) && !(value instanceof DataView) ? (value as TypedArray) : null;
export function isTypedArray(value: unknown): value is TypedArray {
return ArrayBuffer.isView(value) && !(value instanceof DataView);
}

/**
* Check is an array is a numeric array (typed array or array of numbers)
* @param value value to be tested
* @returns input as NumericArray, or null
*/
export function isNumericArray(value: unknown): NumericArray | null {
export function isNumericArray(value: unknown): value is NumericArray {
if (Array.isArray(value)) {
return value.length === 0 || typeof value[0] === 'number' ? (value as number[]) : null;
return value.length === 0 || typeof value[0] === 'number';
}
return isTypedArray(value);
}

0 comments on commit 6b1f058

Please sign in to comment.