#infer type not working properly?

12 messages · Page 1 of 1 (latest)

bold ginkgo
#

I'd expect t to be type number

#

instead it seems its using {case:1} but wrong inferring

#

const x: Untagged<Tagged<number, "u8", { minValue: 0; maxValue: 255 }>> = 0;
this works

#

More cases

slim kilnBOT
#
andreaelektronvolt#0

Preview:```ts
declare const tags: unique symbol
export type Tagged<
BaseType,
Tag extends PropertyKey,
Metadata = void

= BaseType & {
[tags]: {[K in Tag]: Metadata}
}
export type u8 = Tagged<
number,
"u8",
{minValue: 0; maxValue: 255}

export type Untagged<T> = T extends Tagged<
infer Base,
never,
never

? Base | {case: 1}
: T
...```

wind coyote
#

@bold ginkgo Yeah, it seems the infer only works if Tag is actually set to the right type in the infer clause:

#
declare const tags: unique symbol;

type Untag<T, Tags> = T extends (infer Base) & {[tags]: Tags } ? Base : never;

type Test = number & {[tags]: string };
type T1 = Untag<Test, string>; // number
type T2 = Untag<Test, never>; // never
type T3 = Untag<Test, any>; // Test
type T4 = Untag<Test, unknown> // Test
slim kilnBOT
wind coyote
#

Not sure if this is some necessary aspect of how infer works or if this is a bug.

bold ginkgo
#

looks like some bug to me

wind coyote
#

Could you do:

export type Untagged<T> = Omit<T, typeof tags>

instead?

bold ginkgo
#

i ended up like that