Skip to content

feat(types): enhance TypedReturn to support primitive arrays #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

ifyoumakeit
Copy link

Problem

When using get on a dynamic config with a fallback value, TypeScript returning Array<unknown> for all fallbacks of type array. This forced the use of type assertions (as) to get the correct typing, which isn't ideal for type safety.

Solution

Enhanced the TypedReturn type to properly handle arrays of primitive types (string, number, boolean) by recursively processing array element types. This maintains type safety while supporting the full range of JSON-compatible types, including nested arrays of primitives.

Testing

// Primitive arrays
const stringList = config.get("test.stringList", ["foo", "bar"]);       // type: string[]
const numberList = config.get("test.numberList", [1, 2, 3]);            // type: number[]
const booleanList = config.get("test.booleanList", [true, false]);      // type: boolean[]

// Nested arrays
const matrix = config.get("test.matrix", [[1, 2], [3, 4]]);            // type: number[][]
const stringMatrix = config.get("test.stringMatrix", [["a"], ["b"]]);   // type: string[][]

// Mixed arrays
const mixedList = config.get("test.mixed", ["foo", 42, true]);         // type: (string | number | boolean)[]
const stringOrNumber = config.get("test.mixed", ["foo", 42]);          // type: (string | number)[]

// Enum arrays
enum Status { Active = "ACTIVE", Inactive = "INACTIVE" }
const statusList = config.get("test.status", [Status.Active]);          // type: string[]

// Object arrays
const objectList = config.get("test.objects", [{ id: 1 }, { id: 2 }]); // type: object[]

// With undefined (fallback)
const maybeList = config.get("test.maybe");                            // type: unknown
const defaultList = config.get("test.default", [] as string[]);        // type: string[]

// Tuple types
const tuple = config.get("test.tuple", ["id", 123] as [string, number]); // type: [string, number]

…rsion

The TypedReturn type now properly handles arrays of primitive types (string, number, boolean) by recursively processing array element types. This maintains type safety while supporting the full range of JSON-compatible types, including nested arrays of primitives. The implementation uses `T[number]` to extract array element types and applies TypedReturn recursively to preserve type information through array nesting.
@ifyoumakeit
Copy link
Author

ha, closing since this is most likely due to how ya'll check value against the fallback type with _isTypeMatch for typed getters, and don't recurse over the array there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant