GitHub user DadiBit created a discussion: Better type safety schemas
Greetings, I just started exploring Arrow and DuckDB, and I must admit it's quite interesting, however... I was hurting a bit to not be able to get full type safety with typescript intellisense of vscodium. I saw #192, and it looks somewhat related. When I request the fields I just end up with an array of fields... Meh, having a typed object would be much cooler, wouldn't it? Here's how I partially implemented my type safety: ##### `helper.ts` allows me to infer the constructor params as tuples: ```typescript import { Schema, Field } from "@apache-arrow/ts"; // See: https://stackoverflow.com/a/55344772 type Tail<T extends any[]> = T extends [infer A, ...infer R] ? R : never; export type Fields = { [name: string]: Tail<ConstructorParameters<typeof Field>> }; type FieldsTypeMap<T extends Fields> = { [K in keyof T]: T[K][0] }; // Factory to generate typed schema export function schemaFactory<T extends Fields>(fields: T) { const _fields = Object.entries(fields).map(([name, field]) => new Field(name, ...field)); return new Schema<FieldsTypeMap<T>>(_fields); } ``` ##### `student.ts` to check out intellisense ```typescript import { Uint8, Utf8 } from "@apache-arrow/ts"; import { Fields, schemaFactory } from "./helper"; export const fields = { // here it fails to help me with names, but types are checked, that's something! 'name': [new Utf8(), true], 'age': [new Uint8(), true], } satisfies Fields; // satisfies is very import, it doesn't erase the keys! // Now, `schema`, techincally has the generic specific for my type // When using `.fields` you get something out of it: // But it's just Field<Utf8 | Uint8>[]... What the hell, I lose some information, namely the key! export const schema = schemaFactory(fields); ``` So I would like to ask if there's any chance to modify the library to return more meaningful types. This **would break some code**, since the types returned would change and some dev might depend on the type defintions (altough not super useful). GitHub link: https://github.com/apache/arrow-js/discussions/232 ---- This is an automatically sent email for user@arrow.apache.org. To unsubscribe, please send an email to: user-unsubscr...@arrow.apache.org