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

TS language server features for sum type members (e.g. rename) #10

Open
OliverJAsh opened this issue Sep 13, 2021 · 3 comments
Open

TS language server features for sum type members (e.g. rename) #10

OliverJAsh opened this issue Sep 13, 2021 · 3 comments

Comments

@OliverJAsh
Copy link
Member

OliverJAsh commented Sep 13, 2021

Slack discussion for reference: https://crewlabs.slack.com/archives/CFZE45NTW/p1631709631008600

With Unionize it was very easy to rename a tag in the union—TS would propagate the change to all constructors, match functions and type definitions. Can we achieve something close to this?

Screen.Recording.2021-09-13.at.17.55.23.mov
@OliverJAsh
Copy link
Member Author

OliverJAsh commented Sep 13, 2021

Unfortunately I doubt this will be possible.

Renaming works if we define the union first as a record and pass that record around (i.e. the way Unionize works):

type MyRecord = { A: string; B: string };
type Constructors<T> = { [K in keyof T]: () => void };
declare const constructors: Constructors<MyRecord>;
constructors.A();

However it doesn't work if we define a union manually:

type MyUnion = { tag: 'A'; value: string } | { tag: 'B'; value: number };
type Constructors<T extends { tag: string; value: unknown }> = {
    [K in T as K['tag']]: () => void;
};
declare const constructors: Constructors<MyUnion>;
constructors.A();

It seems that renaming only works when you use K in keyof T in a mapped type, not when you do K in T as K['tag'] or K in T['tag'].

@OliverJAsh
Copy link
Member Author

@OliverJAsh
Copy link
Member Author

This is a wider issue than just renaming. It also affects other language server features such as "go to definition" and "find references". Example of go to definition from my reduced test case above:

image

@OliverJAsh OliverJAsh changed the title Renaming union tags TS language server features for sum type members (e.g. rename) Jun 7, 2023
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