From 4d3a13c8192ae924a10cfc15f8c59fa9a48ff352 Mon Sep 17 00:00:00 2001 From: Ruby Quail Date: Sat, 13 Jan 2024 11:29:47 +1100 Subject: [PATCH] Enhance type safety in StructuredView ensuring that more specific TypeDefinitions lead to stricter and more accurate type checks. Signed-off-by: Ruby Quail --- src/buffer-views.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/buffer-views.ts b/src/buffer-views.ts index 3188eeb..b7279e1 100644 --- a/src/buffer-views.ts +++ b/src/buffer-views.ts @@ -384,7 +384,7 @@ export function setStructuredView(data: any, views: TypedArrayOrViews): void { } } -export type StructuredView = ArrayBufferViews & { +export type StructuredView = ArrayBufferViews & { /** * Sets the contents of the TypedArrays based on the data passed in * Note: The data may be sparse @@ -425,7 +425,7 @@ export type StructuredView = ArrayBufferViews & { * * @param data */ - set(data: any): void; + set(data: ViewType extends StructDefinition ? Partial> : any): void; } /** @@ -436,13 +436,13 @@ export type StructuredView = ArrayBufferViews & { * @returns TypedArray views for the various named fields of the structure as well * as a `set` function to make them easy to set, and the arrayBuffer */ -export function makeStructuredView(varDef: VariableDefinition | StructDefinition, arrayBuffer?: ArrayBuffer, offset = 0): StructuredView { +export function makeStructuredView(varDef: InputType, arrayBuffer?: ArrayBuffer, offset = 0): StructuredView { const asVarDef = varDef as VariableDefinition; - const typeDef = asVarDef.group === undefined ? varDef as StructDefinition : asVarDef.typeDefinition; + const typeDef = asVarDef.group === undefined ? varDef as unknown as ViewType : asVarDef.typeDefinition as ViewType; const views = makeTypedArrayViews(typeDef, arrayBuffer, offset); return { ...views, - set(data: any) { + set(data) { setStructuredView(data, views.views); }, };