Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialized type is inlined #1

Open
samhh opened this issue Nov 30, 2021 · 1 comment
Open

Serialized type is inlined #1

samhh opened this issue Nov 30, 2021 · 1 comment

Comments

@samhh
Copy link
Member

samhh commented Nov 30, 2021

getCodecFromSerialized<A>({ A1: t.undefined, A2: t.number }) // $ExpectType Type<A, readonly ["A1", undefined] | readonly ["A2", number], unknown>

Small repro in sum-types repo (to prove it's not (only) because we're reconstructing it):

type Serialized1<A extends Sum.AnyMember> = [Tag<A>, Value<A>] // this preserves the name
type Serialized2<A> = A extends Sum.AnyMember ? [Tag<A>, Value<A>] : never // this does not

type X = Sum.Member<'X', string> | Sum.Member<'Y', number>

declare const x1: Serialized1<X> // aliased
if (x1[0] === "X") x1[1] // string | number
declare const x2: Serialized2<X> // expanded
if (x2[0] === "X") x2[1] // string

We're using the conditional type to distribute over union members.

As an aside we should probably export Serialized for both this and the fp-ts bindings repo. I also wonder if that would remove the need for some of the assertions?

@samhh
Copy link
Member Author

samhh commented Aug 10, 2023

This is a hack, but...:

type Serialized1<A extends Sum.AnyMember> = [Tag<A>, Value<A>]
type Serialized2<A> = A extends Sum.AnyMember ? [Tag<A>, Value<A>] : never
type Serialized3<A> = Serialized2<A> & {} // <- this intersection

type X = Sum.Member<'X', string> | Sum.Member<'Y', number>

declare const x1: Serialized1<X> // aliased
if (x1[0] === "X") x1[1] // string | number
declare const x2: Serialized2<X> // expanded
if (x2[0] === "X") x2[1] // string
declare const x3: Serialized3<X> // aliased
if (x3[0] === "X") x3[1] // string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant