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

__typename must be const #122

Open
captbaritone opened this issue Mar 7, 2024 · 5 comments
Open

__typename must be const #122

captbaritone opened this issue Mar 7, 2024 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@captbaritone
Copy link
Owner

Currently we allow this, but it's not technically safe since the __typename could be mutated.

class User implements IPerson {
  __typename = "User";
}

We should require either:

class User implements IPerson {
  __typename = "User" as const;
}

or

class User implements IPerson {
  __typename: "User" = "User";
}
@Rash-Hit
Copy link
Contributor

Rash-Hit commented Mar 8, 2024

@captbaritone can i take this ?? what all other things needs to in mind while doing this . ???

@captbaritone
Copy link
Owner Author

@Rash-Hit Sorry I missed your message! I think all we need to do here is update the code here to expect an "AsExpression" with an expression that is the expected string literal and a type that is const (I think there's a keyword constant exported by TypeScript somewhere).

Then you should be sure to update/add test fixtures showing the "happy" path as well as demonstrating exactly what the error messages look like. You can read more here: https://github.com/captbaritone/grats/blob/2f7323fb43a31ed1895b394bbdbc01fba583cc0c/CONTRIBUTING.md#automated-tests

@captbaritone captbaritone added bug Something isn't working good first issue Good for newcomers labels Mar 22, 2024
@captbaritone
Copy link
Owner Author

@Rash-Hit Following up here. Are you still interested in working on this? No worries if not.

@Rash-Hit
Copy link
Contributor

i am really sorry @captbaritone , is this something we need to do ??
image

@captbaritone
Copy link
Owner Author

@Rash-Hit Yeah, that looks about right. We probably also need to check that it's as const (look at the .identifier of the as expression. You'll also need to make sure that in the as const case you look for the literal name string as the .expression of the as expression node.

Here's what it looks like in the correct case:
Screenshot 2024-03-23 at 12 22 04 PM

https://astexplorer.net/#/gist/615fac072e169328425c18cb9d88ba66/bea0aafa2f40091c2d62bf65dae3b3834d8bdf0a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants