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

allow constants for string literals types #6151

Closed
zpdDG4gta8XKpMCd opened this issue Dec 18, 2015 · 8 comments
Closed

allow constants for string literals types #6151

zpdDG4gta8XKpMCd opened this issue Dec 18, 2015 · 8 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@zpdDG4gta8XKpMCd
Copy link

const vow = 'will never change';
let promise: vow;
@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Dec 18, 2015
@RyanCavanaugh
Copy link
Member

We can't do this since it would be a breaking change -- vow has not previously introduced an entry in the type namespace, so doing so now would mean random types would start getting shadowed.

typeof vow should do what you want here (if we made consts be of their string literal type @DanielRosenwasser please confirm?).

@zpdDG4gta8XKpMCd
Copy link
Author

typeof vow should do what you want here

in the expression context typeof vow gets me "string"
in the type expression context it gets me "will never change", but it's a string literal type which I cannot assign to a variable

... if we made consts be of their string literal type

if we assuming it's hasn't yet been done, then it answers it

@DanielRosenwasser
Copy link
Member

Like @RyanCavanaugh said, in order to do typeof vow, you'd need #5796. That said, I don't know if that PR will go in. The behavior isn't entirely ideal because it's not clear when you want the literal type and not string.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 15, 2016

Here is the proposal: #6167 (comment)

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed By Design Deprecated - use "Working as Intended" or "Design Limitation" instead labels Jan 15, 2016
@sceutre
Copy link

sceutre commented Nov 30, 2016

What about allowing interpolated strings as string literal types. So:

const vow = "will never change";
let promise: `${vow}`;

@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2016

The issue in the OP should be fixed by #10676

Allowing string interpolation, or any form of string manipulation in type positions is not something we are looking to allow.

for this specific example, use typeof operator, e.g. let promise: typeof vow; and this should be "will never change".

@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed In Discussion Not yet reached consensus labels Dec 1, 2016
@mhegazy mhegazy modified the milestone: TypeScript 2.1.3 Dec 1, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2016

@Aleksey-Bykov do you agree this proposal is covered by #10676 and typeof?

@zpdDG4gta8XKpMCd
Copy link
Author

yes, it's solved, please clise

@mhegazy mhegazy closed this as completed Dec 1, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants