Files
Dannon Baker 47f87c92f3 Fix GetComponentPropTypes for Vue 3 and ListingLayout's :key type
GetComponentPropTypes was written for Vue 2, back when ExtractPropTypes
was unreliable there -- it required a component to already look like a
ComponentPublicInstance, which real Vue 3 SFC types don't satisfy the
same way anymore. Rewrote it around InstanceType<T>["$props"], the
standard Vue 3 way to pull a child component's prop types out of
`typeof Component`. Fixes both of its call sites (CarbonEmissionCard
and CarbonEmissions) at once.

ListingLayout's getKey() could return undefined when the configured
dataKey field was missing from an item, which isn't a valid :key --
falls back to the index in that case, same as when there's no dataKey
configured at all.
2026-10-03 15:27:54 -04:00

56 lines
2.2 KiB
TypeScript

/** length of const array or tuple */
export type Length<T extends any[] | readonly any[]> = T["length"];
/** build a tuple with length L, and type T */
export type Tuple<L extends number, T = any, R extends T[] = []> = R extends { length: L } ? R : Tuple<L, T, [...R, T]>;
/** adds two constant positive whole number types */
export type Add<A extends number, B extends number> = Length<[...Tuple<A>, ...Tuple<B>]>;
/** subtracts one constant positive whole number type from another */
export type Subtract<A extends number, B extends number> =
Tuple<A> extends [...infer U, ...Tuple<B>] ? Length<U> : never;
/** type of the last element of a readonly array or tuple */
export type Last<A extends readonly [...any]> = A[Subtract<Length<A>, 1>];
/** type of the first element of a readonly array or tuple */
export type First<A extends readonly [...any]> = A[Length<A>];
/**
* Extract the prop types of a Vue SFC from its component constructor type.
* @example
* type MyComponentPropsType = GetComponentPropTypes<typeof MyComponent>;
*/
export type GetComponentPropTypes<T extends abstract new (...args: any) => any> = InstanceType<T>["$props"];
/** Convert snake case string literal to camel case string literal */
export type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}`
? `${T}${Capitalize<SnakeToCamelCase<U>>}`
: S;
/** Convert camel case string literal to snake case string literal */
export type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}`
? `${T extends Capitalize<T> ? "_" : ""}${Lowercase<T>}${CamelToSnakeCase<U>}`
: S;
/** Converts an object type or interface to the same type with camel case keys */
export type AsCamelCase<T> =
T extends Array<any>
? T
: T extends object
? {
[K in keyof T as SnakeToCamelCase<K & string>]: AsCamelCase<T[K]>;
}
: T;
/** Converts an object type or interface to the same type with snake case keys */
export type AsSnakeCase<T> =
T extends Array<any>
? T
: T extends object
? {
[K in keyof T as CamelToSnakeCase<K & string>]: AsSnakeCase<T[K]>;
}
: T;