ts-type-explorer icon indicating copy to clipboard operation
ts-type-explorer copied to clipboard

Union of object properties not working

Open swingthrough opened this issue 1 year ago • 1 comments

type Original = {
	propA?: string;
	propB?: string;
};

type Unioned = Original & {
	propB?: string[];
};

Clicking on Unioned doesn't update the type tree with the type.

swingthrough avatar Oct 05 '24 14:10 swingthrough

I'm able to reproduce this. The simplest case seems to just be,

type Union = string & string[];

It crashes with the following error in the console:

TypeTreeRequest error TypeError: Cannot read properties of undefined (reading 'name')

The error occurs here: https://github.com/mxsdev/ts-type-explorer/blob/main/packages/api/src/tree.ts#L438

@mxsdev, could this simply be due to the change in TypeScript 5.2? https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#labeledelementdeclarations-may-hold-undefined-elements

In order to support a mixture of labeled and unlabeled elements, TypeScript’s API has changed slightly. The labeledElementDeclarations property of TupleType may hold undefined for at each position where an element is unlabeled.

I just changed it to this and @swingthrough's test case didn't crash:

    ).labeledElementDeclarations?.map((s) =>
        s ? s.name.getText() : "(unnamed tuple index)"
    ),

(I put "(unnamed tuple index)" just to see where it occurred, but a more proper value would probably be "")

Granted, instead of giving just "undefined", "string", and "string[]", I also got all inherited properties of an array. (But maybe this is correct?)

Image

...

Image

EDIT:

Apparently it's coincidential that this error is occurring for string & string[] and only happens because it generates a nice big tree, and deep inside that tree, there are occurrences of tuples:

Image

The real issue is with tuples in general, as #55 points out.

CodeSmith32 avatar Jul 24 '25 19:07 CodeSmith32