Get the latest tech news

Trying to Understand Copilot's Type Spaghetti


f Typescript generated by Github copilot was floating around: type MappedParameterTypes<T extends Parameter[]> = { // Check if the parameter has an 'enum' defined [P in T[number] as P["name"]]: P extends {enum: Array<infer E> } ? E extends string // Ensure the enum values are strings ? P["required"] extends false // Check if the parameter is optional ? E | undefined // If so, include 'undefined' in the type : E // Othewise use the enum type directly : never // This case should not occur since 'enum' implies string values : // Handle parameters defined as 'object' with specified attributes P extends {type: "object"; attributes: infer Attributes } ? Attributes extends Parameter[] ? MappedParameterTypes<Attributes> // Recursively map the attributes of the object : never // If 'attributes' is not an array of Parameters, this is invalid : // Handle parameters defined as 'object[]' without specified attributes P extends { type: "object[]"; attributes?: never } ? any[] // Default to 'any[]' for arrays of objects without specific attributes : P extends {type: "object[]"; attributes: infer Attributes } ? Attributes extends Parameter[] ? MappedParameterTypes<Attributes>[] // Recursively map each object in the array : any[] // Default to 'any[]' if attributes are not properly defined : // Handle all other parameter types P["required"] extends false ? // Include 'undefined' for optional parameters TypeMap[P["type"] extends keyof TypeMap ? P["type"] : "string"] | undefined : // Use the direct mapping from 'TypeMap' for the parameter's type TypeMap[P["type"] extends keyof TypeMap ? P["type"] : "string"]; }; This is some type-level programming that can transform an array of field definitions into a type. The above is the original code, and I was able to fill in the blanks for TypeMap and Parameter with the following: interface Parameter { name: string, required?: false, type?: keyof TypeMap } interface TypeMap { number: number, boolean: boolean, string: string, } The first question is how do you use something like MappedParameterTypes? I've definitely seen this code in many projects, especially those with bespoke ORMs.

I similary find that the P["requires"] extends false check being placed somewhat haphazardly leads to weird usability issues. But if you want something that is easy to maintain, that unfortunately often requires a lot of pedantic work and putting things into generic types. For example, here, decoupling the naming and the requiredness from the type development might lead to more lines of codes, but that are easier to grok.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Copilot

Copilot

Photo of type spaghetti

type spaghetti

Related news:

News photo

Microsoft's Copilot image tool generates ugly Jewish stereotypes, anti-Semitic tropes

News photo

Microsoft 365’s Copilot gets a GPT-4 Turbo upgrade and improved image generation

News photo

Microsoft upgrades Copilot for 365 with GPT-4 Turbo, unlimited messaging, 100 image gen boosts, and more