Skip to content

Literal assertions #14161

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

Closed
wants to merge 3 commits into from
Closed

Literal assertions #14161

wants to merge 3 commits into from

Conversation

gcnew
Copy link
Contributor

@gcnew gcnew commented Feb 18, 2017

Fixes #14156

Disallow unrelated literal type assertions. E.g.

0 as 0; // OK
0 as 1; // Error

false as (false | string); // OK
false as (true | string);  // Error
false as (boolean | string);  // OK

'hello' as 'str'; // Error
'hello' as 'hello'; // OK
'hello' as ('str' | 123); // Error
'hello' as ('hello' | 123); // OK
'hello' as ('str' & 'hello'); // OK
'hello' as ('str' | 'hello'); // OK
'hello' as (1 | 2 | string); // OK
'hello' as ('str' & { _brand: any }); // Error
'hello' as ('hello' & { _brand: any }); // OK
'hello' as ('str' & { _brand: any } | 1); // Error
'hello' as ('hello' & { _brand: any } | 1); // OK

('string' as string as 'literal'); // OK

@mhegazy
Copy link
Contributor

mhegazy commented May 22, 2017

closing as per #14156 (comment)

@Ghabriel
Copy link

Shouldn't this be reopened, considering the discussion at #22038?

@laughinghan
Copy link

laughinghan commented Mar 9, 2018

#14156 (comment) makes sense, but only applies to expressions where there's potentially something the compiler doesn't know. #22038 could be ameliorated with a rule like this that only applies to actual syntactic literals, not just expressions that evaluate to a literal type. In other words:

var x: "asdf" = "asdf";
var y = x as "lol"; // fine, allow this I guess for reasons described in #14156

var z = "asdf" as "lol"; // don't allow this!!!

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants