Skip to content

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