From 4997d16c3ce5f485d043fa7c4ef47b5892d30daf Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Sun, 21 Jul 2024 01:41:08 -0700 Subject: [PATCH] fix CI 2 --- .../lib/api/playerActionsPayloadsProviders.ts | 1 + web/test/lib_tests/scratchpad2.test.ts | 35 ------------ web/test/lib_tests/scratchpad3.test.ts | 54 ------------------- 3 files changed, 1 insertion(+), 89 deletions(-) delete mode 100644 web/test/lib_tests/scratchpad2.test.ts delete mode 100644 web/test/lib_tests/scratchpad3.test.ts diff --git a/web/src/lib/api/playerActionsPayloadsProviders.ts b/web/src/lib/api/playerActionsPayloadsProviders.ts index f7a8751..21827f9 100644 --- a/web/src/lib/api/playerActionsPayloadsProviders.ts +++ b/web/src/lib/api/playerActionsPayloadsProviders.ts @@ -49,6 +49,7 @@ const actionNameToPayloadProviderFactoryMap: { // Related: // https://stackoverflow.com/questions/49099224/passing-a-function-accepting-fewer-parameters-how-to-enforce-the-same-number-o // https://stackoverflow.com/questions/27336393/is-there-a-way-to-make-typescript-consider-function-types-non-equivalent-when-th +// kja2-refactor: deduplicate per answer to: https://stackoverflow.com/questions/78739104/how-can-i-avoid-duplicating-key-in-value-and-duplicating-type-and-const-definit type PayloadProviderMap = // prettier-ignore { diff --git a/web/test/lib_tests/scratchpad2.test.ts b/web/test/lib_tests/scratchpad2.test.ts deleted file mode 100644 index 1c4777b..0000000 --- a/web/test/lib_tests/scratchpad2.test.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unsafe-call */ -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/no-unsafe-return */ -/* eslint-disable id-length */ -/* eslint-disable lodash/prefer-lodash-method */ -// https://stackoverflow.com/questions/78739104/how-can-i-avoid-duplicating-key-in-value-and-duplicating-type-and-const-definit -/* eslint-disable @typescript-eslint/no-unused-vars */ -import _ from 'lodash' -type Name = 'foo' | 'bar' - -type ProviderFuncFromString = (data: string) => { name: Name; data: string } -type ProviderFuncFromNumber = (numb: number) => { name: Name; data: string } - -function providerFuncFromStringFactory(name: Name): ProviderFuncFromString { - return (data) => ({ name, data }) -} - -function providerFuncFromNumberFactory(name: Name): ProviderFuncFromNumber { - return (numb) => ({ name, data: numb.toString() }) -} - -function mappy(m: { [K in keyof T]: (name: K) => T[K] }): T { - return Object.fromEntries( - Object.entries(m).map(([k, v]) => [k, (v as any)(k)]), - ) as any -} - -const providerMap = mappy({ - foo: providerFuncFromStringFactory, - bar: providerFuncFromNumberFactory, -}) -type ProviderMap = typeof providerMap -// ^? - -const xyz = providerMap.foo('bar') diff --git a/web/test/lib_tests/scratchpad3.test.ts b/web/test/lib_tests/scratchpad3.test.ts deleted file mode 100644 index 51cdba2..0000000 --- a/web/test/lib_tests/scratchpad3.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unsafe-call */ -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/no-unsafe-return */ -/* eslint-disable id-length */ -/* eslint-disable lodash/prefer-lodash-method */ -// https://stackoverflow.com/questions/78739104/how-can-i-avoid-duplicating-key-in-value-and-duplicating-type-and-const-definit -/* eslint-disable @typescript-eslint/no-unused-vars */ -import _ from 'lodash' -type StringAction = 'foo' -type NumberAction = 'bar' | 'buz' -type Name = StringAction | NumberAction - -type ProviderFuncFromString = (data: string) => { - name: K - data: string -} -type ProviderFuncFromNumber = (numb: number) => { - name: K - data: string -} - -function providerFuncFromStringFactory( - name: K, -): ProviderFuncFromString { - return (data: string) => ({ name, data: String(data) }) -} - -function providerFuncFromNumberFactory( - name: K, -): ProviderFuncFromNumber { - return (numb) => ({ name, data: numb.toString() }) -} - -type ActionToFunctionMap = { - [K in Name]: K extends StringAction - ? ProviderFuncFromString - : K extends NumberAction - ? ProviderFuncFromNumber - : never -} - -const providerMap: ActionToFunctionMap = { - foo: providerFuncFromStringFactory('foo'), - bar: providerFuncFromNumberFactory('bar'), - buz: providerFuncFromNumberFactory('buz'), -} - -type Action = keyof ActionToFunctionMap -const action: Action = 'bar' -console.log(providerMap[action](4)) - -const xyz = providerMap.bar(10) - -console.log(xyz)