-
Notifications
You must be signed in to change notification settings - Fork 14
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
UX issues when inspecting types (truncation, type aliases not used) #60
Comments
Observation 3: import { ofType, unionize, UnionOf } from 'unionize';
const MyUnion = unionize({
Foo: ofType<{ foo: number; bar: number; baz: number }>(),
});
MyUnion.Foo();
|
Regarding observation 1, type Foo = { foo: number };
type Bar = { bar: number };
const MyUnion = unionize(
{
Foo: ofType<Foo>(),
// Bar: ofType<Bar>(),
}
);
// Hover this
type MyUnion = UnionOf<typeof MyUnion>; Hovering over {
tag: "Foo";
foo: number;
} … but enable Compact<{
tag: "Foo";
} & Foo> | Compact<{
tag: "Bar";
} & Bar> 😢 Regarding observation 2, TypeScript issue for this: microsoft/TypeScript#32287 Regarding observation 3
|
Regarding these issues:
and
It seems the problem happens when we use -type MyUnion = UnionOf<typeof MyUnion>;
+type MyUnionTaggedRecord = TaggedRecordOf<typeof MyUnion>;
+type MyUnion = MyUnionTaggedRecord[keyof MyUnionTaggedRecord]; Complete example: import { ofType, unionize, TaggedRecordOf } from "unionize";
type Foo = { foo: number };
type Bar = { bar: number };
type Baz = { baz: number };
const MyNestedUnion = unionize(
{
NestedFoo: ofType<Foo>(),
NestedBar: ofType<Bar>(),
NestedBaz: ofType<Baz>(),
},
{ value: 'value' },
);
type MyNestedUnionTaggedRecord = TaggedRecordOf<typeof MyNestedUnion>;
type MyNestedUnion = MyNestedUnionTaggedRecord[keyof MyNestedUnionTaggedRecord];
const MyUnion = unionize(
{
Foo: ofType<Foo>(),
Bar: ofType<Bar>(),
Baz: ofType<Baz>(),
Union: ofType<MyNestedUnion>(),
},
{ value: 'value' },
);
type MyUnionTaggedRecord = TaggedRecordOf<typeof MyUnion>;
// Hover this
type MyUnion = MyUnionTaggedRecord[keyof MyUnionTaggedRecord];
// Hover this
declare const fn: (f: MyUnion) => void; Perhaps we should discourage |
We're using Unionize pretty heavily at Unsplash (thank you!). I would like to share some UX feedback relating to how the union types appear when inspected. To do so, I will use an example.
Vanilla tagged union
First let's see how type inspection works for a sans-Unionize union:
Hover over
type MyUnion
and you'll see:Great! Hover over
const fn
and you'll see:Great! No problems here.
Unionize union
Now if we define the equivalent tagged union but using Unionize:
Now hover over
type MyUnion
and you'll see:Observations:
Compact
(which "cleans" types with lots of arithmetic)?MyNestedUnion
type alias was shown nested inside of here. However in this Unionize example, the type aliasMyNestedUnion
is thrown away and the whole type structure is shown instead. Why? Is this a design limitation or bug in TS? Is it something we can workaround?Hover over
const fn
and you'll see:In the sans-Unionize example, the
MyUnion
type alias was shown here. However in this Unionize example, the type aliasMyUnion
is thrown away and the whole type structure is shown instead. Why?I appreciate there might not be much we can do from Unionize's side to help address these UX issues. However I imagine there's at least some discussions inside of TS we could chime into, so our voice is heard. 🤞
The text was updated successfully, but these errors were encountered: