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

Feature Request: Declare type conformance with "as const" #33262

Closed
kolodny opened this issue Sep 5, 2019 · 4 comments
Closed

Feature Request: Declare type conformance with "as const" #33262

kolodny opened this issue Sep 5, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@kolodny
Copy link

kolodny commented Sep 5, 2019

This is a feature request for #32043

I want to reach for as const often but also want to ensure a variable matches a specific type.

const config = {
  port: 8080,
  nested: {
    value: 123,
  },
} as const;

However, I lose the piece of mind knowing I didn't make a typo when creating that object so I often opt for the typed variant

const config: Config = {
  port: 8080,
  nested: {
    value: 123,
  },
};

It would be nice to have syntax to allow creating a unwidened type while also knowing that the variable matches another interface. Syntax like the following would allow me to do both:

const config = {
  port: 8080,
  nested: {
    value: 123,
  },
} as const<Config>;

A helper function wouldn't work since you'd either need to extend some T allowing extra properties on the object which should not be allowed; or you need to match T exactly which causes the type to be widened:

https://typescript.googleplex.com/?codez=lZBNDoIwGET3PcXshIRALK7423gFPQDgp2CwNaUaE%2BPdbSkgGxYu2%2Bm8N2ktRa%2FRUHcnhRzZofB0goOPvIBOWb2I%2BZgPWXYEvTSJUw9XOU4V1gpN6lzWhL0U5%2FaCNwPuUplH4nGrSKXmLKjXdEqGDHiW3YOm2Nx82IeNblldt0bsJmSOWHg%2F5JbHwRqPx7vA0VD2GHi%2BGWiRoW0jikYnEm8h5LOQz0b%2FPyfs96gywUY2EJI2K0N46Cjh8NwOmgp2EvAF

There is an argument to be made that when the variable is used in a function, that function should be typed, which should catch any typos or type issues. That's not always possible since the variable can be sent to an angular template, effectively losing all typings or may be passed to a library/function that isn't well typed

@AnyhowStep
Copy link
Contributor

#31062

@sandersn
Copy link
Member

sandersn commented Sep 5, 2019

@AnyhowStep I agree, this is basically a duplicate of #31062.

@kolodny as Config might do what you want, but I assume you considered that already?

@sandersn sandersn added the Duplicate An existing issue was already created label Sep 5, 2019
@AnyhowStep
Copy link
Contributor

AnyhowStep commented Sep 5, 2019

as Config is unsafe, though. He's (probably) asking for a version that checks that it actually conforms to the shape of Config, rather than an unsafe cast.

Of course, I could be completely wrong and he's asking for as ReadOnly<Config>

@kolodny
Copy link
Author

kolodny commented Sep 5, 2019

@sandersn Yup looks like a dupe, I'll close this bug.

as Config would widen the type so port would then be number and not 8080, what I was asking (and what #31062) is asking is a way to use as const but also have the piece of mind knowing the object correctly conforms to a specific type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants