#Intersection Type between an unknown type & known type?

49 messages · Page 1 of 1 (latest)

azure pelicanBOT
#

@simple bone Here's a shortened URL of your playground link! You can remove the full link from your message.

krauti#0

Preview:```ts
export enum settingNames {
"foo",
"bar",
}

export interface iSetting<t> {
name: string
value: t
}

export class Setting<t> implements iSetting<t> {
name: keyof typeof settingNames
value: t

constructor(
name: keyof ty
...```

fresh dagger
#

I guess i could add a generic SettingManager<t = {}> somehow.
yes, sounds like you want to make SettingManager generic. are you saying that all SettingManager instances will have a common set of base settings that they require?

simple bone
#

exactly

fresh dagger
#

do all settings always have the same value type? i see it's just unknown there, but it seems like you could be more specific

#

related question: does the storage have to be in a Map?

simple bone
#

no it was just my second attempt to get the type done right

simple bone
fresh dagger
#

do you care to do any kind of runtime validation for the setting values? or do you just want to trust that whatever was stored in the DB is legit?

simple bone
#

i see where youre getting it comes down to a getter function or no getter function

#

i think at this point i would like to check it

#

i think another approach would be to extend the whole manager with a customVersion which needs to be provided at runtime

fresh dagger
#

i'm just brainstorming here, but:

azure pelicanBOT
#
mkantor#0

Preview:```ts
type SettingsSchema = Record<
string,
(
value: unknown
) => value is string | boolean | number

type SettingsFromSchema<S extends SettingsSchema> = {
[K in keyof S]: S[K] extends (
value: unknown
) => value is infer T
? T
: never
}

export class SettingManager<S extends S
...```

fresh dagger
#

that doesn't lock in any baseline settings, but you could add such a thing in

#

i guess it could be simpler if you know for sure you only ever want to allow string | boolean | number. the schema could be like { foo: 'string' } or whatnot then instead of having callers provide the type guard themselves

#

but maybe you want it to be more flexible ¯_(ツ)_/¯

simple bone
#

any reason for accessing the key as string?

fresh dagger
#

what do you mean?

simple bone
#

const fooValue = settingManager.settings?.['foo'] vs const fooValue = settingManager.settings?.foo

fresh dagger
#

oh like settings?.['foo']? no i don't know why i wrote it that way 😂

#

probably just because i evolved that from your function call

simple bone
#

and how would describe baseline settings here now?

fresh dagger
#

you might still want to encapsulate lookup behind a method, i was just being lazy

simple bone
#

the interesting part happens in the first 3 rows hehe

fresh dagger
simple bone
#

would you care to make an example?

azure pelicanBOT
#
mkantor#0

Preview:```ts
type SettingsSchema = Record<
string,
(
value: unknown
) => value is string | boolean | number

type SettingsFromSchema<S extends SettingsSchema> = {
[K in keyof S]: S[K] extends (
value: unknown
) => value is infer T
? T
: never
}

class SettingManager<S extends S
...```

simple bone
#

okay yeah i tried that but i was not sure about it since it seems very "unhandy"

fresh dagger
#

☝️ that uses inheritance, but you could also consider composition (keep an internal property keeping track of the underlying SettingManager

simple bone
#

well for each key i would add another line to the object correct?

#

like

#

wait

#

here

export const settingManager = new SettingManagerWithRequiredFooBar({ baz: (value): value is boolean => typeof value === 'boolean' })

#

could i make a const ```
const customKeys = {
baz: (value): value is boolean => typeof value === 'boolean',
hugo: (value): value is boolean => typeof value === 'string'
}

#

and use it

fresh dagger
#

like i said that could be new SettingManagerWithRequiredFooBar({ baz: 'boolean' }) or similar if you want to make assumptions about the types

simple bone
#

export const settingManager = new SettingManagerWithRequiredFooBar(someKEys)

fresh dagger
simple bone
#

i think to truly understand i would need to read about is and infer havent used that

#

thanks for now

fresh dagger
fresh dagger
azure pelicanBOT
#
mkantor#0

Preview:```ts
type TypesByName = {
string: string
boolean: boolean
number: number
}
type SettingsSchema = Record<
string,
"string" | "boolean" | "number"

type SettingsFromSchema<S extends SettingsSchema> = {
[K in keyof S]: S[K] extends (
value: unknown
) => value is infer T
? T
: never
...```

fresh dagger
simple bone
#

but rn i need to just push forward sadly i dont get the time to really geto deeper in typing