You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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:
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
The text was updated successfully, but these errors were encountered:
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>
@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
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.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
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:
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 matchT
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
The text was updated successfully, but these errors were encountered: