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

Type assertion for string literals stopped working #10863

Closed
normalser opened this issue Sep 12, 2016 · 3 comments
Closed

Type assertion for string literals stopped working #10863

normalser opened this issue Sep 12, 2016 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@normalser
Copy link

TypeScript Version: 2.1.0-dev.20160912

Code

// A *self-contained* demonstration of the problem follows...
const types = {
    test: <'test'>'test',
    test2: 'test2' as 'test2'
} // type: {test: string, test2:string}

let a = <'test'>'test'  // type string

Probably related to: #10676

@ahejlsberg
Copy link
Member

Yes, this is related to #10676. Previously, a string literal would have a literal type only in certain "literal contexts" (such as after a type assertion) and we would just use the exact type of an expression as the inferred type of an object literal property. Now, string literals always have literal types and instead we widen types when they're inferred for mutable locations (such as object literal properties). So, in your example, the type assertions have no effect (a) because the string literals are already of literal types and (b) because we widen the result of the entire expression after the type assertion.

For better backwards compatibility we could consider not widening an inferred type when the initializer expression is a type assertion. After all, what would be the point of the type assertion otherwise?

@normalser
Copy link
Author

normalser commented Sep 12, 2016

@ahejlsberg Thank you for the response.

It would be great if you could consider not widening an inferred type when the initializer expression is a type assertion - and agree - that's kind of the point of the type assertion :)

Another common case that will stop working without it is:

function test(style: { align: 'left' | 'right', size: number }) {
}

const style = {
    align: 'left' as 'left',
    size: 5
}

test(style)

Syntax for literal types #10195 would help - but I'm not sure if it will ever become a reality

Thanks a lot for everything

@ahejlsberg
Copy link
Member

@wallverb Agreed, it would be nice to have some form of syntax to indicate you don't want widening without having to explicitly state the type. But regardless, for backwards compatibility reasons we should allow type assertions to have that meaning.

@mhegazy mhegazy added the Bug A bug in TypeScript label Sep 12, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Sep 12, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Sep 13, 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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants