From 19a041e4106e35bf3078c28ecae15c5e13697ad1 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Mon, 11 Mar 2024 22:41:55 +0000 Subject: [PATCH 1/5] layout.d.ts: Fix ExtractIds --- typescript/types/layout.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescript/types/layout.d.ts b/typescript/types/layout.d.ts index 167ede29d..fc2e9f50d 100644 --- a/typescript/types/layout.d.ts +++ b/typescript/types/layout.d.ts @@ -3,7 +3,7 @@ type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; type ExtractIds = [Depth] extends [never] ? never - : (T extends { id: infer Id extends string } + : (T extends { id?: infer Id extends string } ? { [k in Id]: T } : never) | From 6ff12f54cf4fcebc3e0c8dde2165214f0e0e293c Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Mon, 11 Mar 2024 22:46:35 +0000 Subject: [PATCH 2/5] layout.d.ts: Layout type - merge extracted ids union --- typescript/types/layout.d.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/typescript/types/layout.d.ts b/typescript/types/layout.d.ts index fc2e9f50d..2810026ca 100644 --- a/typescript/types/layout.d.ts +++ b/typescript/types/layout.d.ts @@ -1,5 +1,8 @@ type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; +type UnionToIntersection = + (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never + type ExtractIds = [Depth] extends [never] ? never @@ -15,7 +18,7 @@ type ExtractIds = declare module Layout { type Layouter = - ExtractIds + UnionToIntersection> & { // these actually change T From b04bc434c91d27d93509a682ec9592d9d13fc4ff Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 16 Mar 2024 13:07:45 +0000 Subject: [PATCH 3/5] layout.d.ts: correctly assign { [id]: T } for layout elements --- typescript/types/layout.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/typescript/types/layout.d.ts b/typescript/types/layout.d.ts index 2810026ca..58aa98ecd 100644 --- a/typescript/types/layout.d.ts +++ b/typescript/types/layout.d.ts @@ -7,7 +7,7 @@ type ExtractIds = [Depth] extends [never] ? never : (T extends { id?: infer Id extends string } - ? { [k in Id]: T } + ? { [k in Id]: { -readonly [P in keyof T]: T[P] } } : never) | ( @@ -34,6 +34,7 @@ declare module Layout { setUI(): void; }; + // Note: you must use new Layout({...} as const) to have ids inferred var Layout: { new ( hier: T, From 030765d409f1f0dcd43fd71736c85f2fa18bc1f8 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 16 Mar 2024 13:06:09 +0000 Subject: [PATCH 4/5] layout.d.ts: permit assignment back to strings ... by making them strings instead of a string literal type --- typescript/types/layout.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescript/types/layout.d.ts b/typescript/types/layout.d.ts index 58aa98ecd..55ddd7135 100644 --- a/typescript/types/layout.d.ts +++ b/typescript/types/layout.d.ts @@ -7,7 +7,7 @@ type ExtractIds = [Depth] extends [never] ? never : (T extends { id?: infer Id extends string } - ? { [k in Id]: { -readonly [P in keyof T]: T[P] } } + ? { [k in Id]: { -readonly [P in keyof T]: T[P] extends string ? string : T[P] } } : never) | ( From 1ed92b08f47a10f73ea35a095afdd3b4f7a1eb67 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sat, 16 Mar 2024 13:09:58 +0000 Subject: [PATCH 5/5] rep: work with better layout types --- apps/rep/app.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/rep/app.ts b/apps/rep/app.ts index d1dd67179..14fb45482 100644 --- a/apps/rep/app.ts +++ b/apps/rep/app.ts @@ -196,7 +196,7 @@ const layout = new L({ ] } ] -}, {lazy: true}); +} as const, {lazy: true}); class State { 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]; if(rep) layout[`${id}_name`]!.label = `${rep.label} / ${msToMinSec(rep.dur)}`; @@ -256,7 +256,7 @@ const repToLabel = (i: number, id: string) => { emptyLabel(id); }; -const emptyLabel = (id: string) => { +const emptyLabel = (id: "cur" | "next") => { layout[`${id}_name`]!.label = " / 0m"; };