Created
March 14, 2024 21:39
-
-
Save jodylecompte/733a66eff79ac2c15507e1d8df64c5fc to your computer and use it in GitHub Desktop.
Original definition of MappedParameterTypes in CopilotKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export 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 // Otherwise, 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 | |
: // Handle parameters defined as 'object[]' with specified 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"]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment