-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Konrad Jamrozik
committed
Jul 21, 2024
1 parent
c056ed4
commit ee61a80
Showing
3 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |