Merge pull request #3252 from bobrippling/fix/layout-ts

Fix typescript for `Layout`
master
Rob Pilling 2024-03-20 08:34:37 +00:00 committed by GitHub
commit 10072d745e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 6 deletions

View File

@ -196,7 +196,7 @@ const layout = new L({
] ]
} }
] ]
}, {lazy: true}); } as const, {lazy: true});
class State { class State {
paused: boolean = true; paused: boolean = true;
@ -248,7 +248,7 @@ class State {
} }
} }
const repToLabel = (i: number, id: string) => { const repToLabel = (i: number, id: "cur" | "next") => {
const rep = reps[i]; const rep = reps[i];
if(rep) if(rep)
layout[`${id}_name`]!.label = `${rep.label} / ${msToMinSec(rep.dur)}`; layout[`${id}_name`]!.label = `${rep.label} / ${msToMinSec(rep.dur)}`;
@ -256,7 +256,7 @@ const repToLabel = (i: number, id: string) => {
emptyLabel(id); emptyLabel(id);
}; };
const emptyLabel = (id: string) => { const emptyLabel = (id: "cur" | "next") => {
layout[`${id}_name`]!.label = "<none> / 0m"; layout[`${id}_name`]!.label = "<none> / 0m";
}; };

View File

@ -1,10 +1,13 @@
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
type ExtractIds<T extends Layout.Hierarchy, Depth extends Prev[number] = 9> = type ExtractIds<T extends Layout.Hierarchy, Depth extends Prev[number] = 9> =
[Depth] extends [never] [Depth] extends [never]
? never ? never
: (T extends { id: infer Id extends string } : (T extends { id?: infer Id extends string }
? { [k in Id]: T } ? { [k in Id]: { -readonly [P in keyof T]: T[P] extends string ? string : T[P] } }
: never) : never)
| |
( (
@ -15,7 +18,7 @@ type ExtractIds<T extends Layout.Hierarchy, Depth extends Prev[number] = 9> =
declare module Layout { declare module Layout {
type Layouter<T extends Hierarchy> = type Layouter<T extends Hierarchy> =
ExtractIds<T> UnionToIntersection<ExtractIds<T>>
& &
{ {
// these actually change T // these actually change T
@ -31,6 +34,7 @@ declare module Layout {
setUI(): void; setUI(): void;
}; };
// Note: you must use new Layout({...} as const) to have ids inferred
var Layout: { var Layout: {
new <T extends Hierarchy>( new <T extends Hierarchy>(
hier: T, hier: T,