Skip to content

Commit

Permalink
fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jul 21, 2024
1 parent c056ed4 commit ee61a80
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
8 changes: 4 additions & 4 deletions web/test/lib_tests/scratchpad.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// 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'
import { describe, expect, test } from 'vitest'
import { z } from 'zod'
Expand Down Expand Up @@ -28,6 +27,8 @@ import { z } from 'zod'
// bar: providerFuncFromNumberFactory('bar'),
// }

// -------------

const NameSchema = z.enum(['foo', 'bar'])

const ProviderFuncFromStringSchema = z
Expand Down Expand Up @@ -76,7 +77,6 @@ const providerMap1 = {
bar: providerFuncFromNumberFactory('bar'),
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const providerMap2 = Object.fromEntries([
['foo', providerFuncFromNumberFactory('foo')],
['bar', providerFuncFromNumberFactory('bar')],
Expand All @@ -94,8 +94,8 @@ describe('scratchpad tests', () => {
const res3 = ProviderMapSchema.safeParse(providerMap3)
expect([res1.success, res2.success, res3.success]).toBe([
true,
false,
false,
true, // I was hoping for this to be false
true, // I was hoping for this to be false
])
})
})
35 changes: 35 additions & 0 deletions web/test/lib_tests/scratchpad2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* 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<T extends object>(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')
54 changes: 54 additions & 0 deletions web/test/lib_tests/scratchpad3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* 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<K extends StringAction> = (data: string) => {
name: K
data: string
}
type ProviderFuncFromNumber<K extends NumberAction> = (numb: number) => {
name: K
data: string
}

function providerFuncFromStringFactory<K extends StringAction>(
name: K,
): ProviderFuncFromString<K> {
return (data: string) => ({ name, data: String(data) })
}

function providerFuncFromNumberFactory<K extends NumberAction>(
name: K,
): ProviderFuncFromNumber<K> {
return (numb) => ({ name, data: numb.toString() })
}

type ActionToFunctionMap = {
[K in Name]: K extends StringAction
? ProviderFuncFromString<K>
: K extends NumberAction
? ProviderFuncFromNumber<K>
: 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)

0 comments on commit ee61a80

Please sign in to comment.