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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10053,7 +10053,7 @@ namespace ts {
}
let type: FlowType;
if (flow.flags & FlowFlags.AfterFinally) {
// block flow edge: finally -> pre-try (for larger explanation check comment in binder.ts - bindTryStatement
// block flow edge: finally -> pre-try (for larger explanation check comment in binder.ts - bindTryStatement
(<AfterFinallyFlow>flow).locked = true;
type = getTypeAtFlowNode((<AfterFinallyFlow>flow).antecedent);
(<AfterFinallyFlow>flow).locked = false;
Expand Down Expand Up @@ -10221,7 +10221,7 @@ namespace ts {
let seenIncomplete = false;
for (const antecedent of flow.antecedents) {
if (antecedent.flags & FlowFlags.PreFinally && (<PreFinallyFlow>antecedent).lock.locked) {
// if flow correspond to branch from pre-try to finally and this branch is locked - this means that
// if flow correspond to branch from pre-try to finally and this branch is locked - this means that
// we initially have started following the flow outside the finally block.
// in this case we should ignore this branch.
continue;
Expand Down Expand Up @@ -14320,12 +14320,20 @@ namespace ts {
}

function checkAssertion(node: AssertionExpression) {
const exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(node.expression)));
const rawType = checkExpression(node.expression);

checkSourceElement(node.type);
const targetType = getTypeFromTypeNode(node.type);

if (produceDiagnostics && targetType !== unknownType) {
const rawLiteralFlags = rawType.flags & (TypeFlags.StringLiteral | TypeFlags.NumberLiteral | TypeFlags.BooleanLiteral);
const check = (type: Type): boolean => !!(type.flags & rawLiteralFlags ||
type.flags & TypeFlags.UnionOrIntersection && some((<UnionOrIntersectionType>type).types, check));

const exprType = rawLiteralFlags && check(targetType)
? rawType
: getRegularTypeOfObjectLiteral(getWidenedLiteralType(rawType));

const widenedType = getWidenedType(exprType);
if (!isTypeComparableTo(targetType, widenedType)) {
checkTypeComparableTo(exprType, targetType, node, Diagnostics.Type_0_cannot_be_converted_to_type_1);
Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/literalAssertions.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
tests/cases/conformance/expressions/typeAssertions/literalAssertions.ts(2,1): error TS2352: Type '0' cannot be converted to type '1'.
tests/cases/conformance/expressions/typeAssertions/literalAssertions.ts(5,1): error TS2352: Type 'false' cannot be converted to type 'string | true'.
tests/cases/conformance/expressions/typeAssertions/literalAssertions.ts(8,1): error TS2352: Type '"hello"' cannot be converted to type '"str"'.
tests/cases/conformance/expressions/typeAssertions/literalAssertions.ts(10,1): error TS2352: Type '"hello"' cannot be converted to type '"str" | 123'.
tests/cases/conformance/expressions/typeAssertions/literalAssertions.ts(15,1): error TS2352: Type '"hello"' cannot be converted to type '"str" & { _brand: any; }'.
Type '"hello"' is not comparable to type '"str"'.
tests/cases/conformance/expressions/typeAssertions/literalAssertions.ts(17,1): error TS2352: Type '"hello"' cannot be converted to type '1 | ("str" & { _brand: any; })'.


==== tests/cases/conformance/expressions/typeAssertions/literalAssertions.ts (6 errors) ====
0 as 0; // OK
0 as 1; // Error
~~~~~~
!!! error TS2352: Type '0' cannot be converted to type '1'.

false as (false | string); // OK
false as (true | string); // Error
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2352: Type 'false' cannot be converted to type 'string | true'.
false as (boolean | string); // OK

'hello' as 'str'; // Error
~~~~~~~~~~~~~~~~
!!! error TS2352: Type '"hello"' cannot be converted to type '"str"'.
'hello' as 'hello'; // OK
'hello' as ('str' | 123); // Error
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2352: Type '"hello"' cannot be converted to type '"str" | 123'.
'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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2352: Type '"hello"' cannot be converted to type '"str" & { _brand: any; }'.
!!! error TS2352: Type '"hello"' is not comparable to type '"str"'.
'hello' as ('hello' & { _brand: any }); // OK
'hello' as ('str' & { _brand: any } | 1); // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2352: Type '"hello"' cannot be converted to type '1 | ("str" & { _brand: any; })'.
'hello' as ('hello' & { _brand: any } | 1); // OK

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

41 changes: 41 additions & 0 deletions tests/baselines/reference/literalAssertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [literalAssertions.ts]
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


//// [literalAssertions.js]
0; // OK
0; // Error
false; // OK
false; // Error
false; // OK
'hello'; // Error
'hello'; // OK
'hello'; // Error
'hello'; // OK
'hello'; // OK
'hello'; // OK
'hello'; // OK
'hello'; // Error
'hello'; // OK
'hello'; // Error
'hello'; // OK
'string'; // OK
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
tests/cases/conformance/types/literal/stringLiteralsAssertionsInEqualityComparisons02.ts(3,9): error TS2365: Operator '===' cannot be applied to types '"foo"' and '"baz"'.
tests/cases/conformance/types/literal/stringLiteralsAssertionsInEqualityComparisons02.ts(3,19): error TS2352: Type '"bar"' cannot be converted to type '"baz"'.
tests/cases/conformance/types/literal/stringLiteralsAssertionsInEqualityComparisons02.ts(4,20): error TS2352: Type '"bar"' cannot be converted to type '"foo"'.
tests/cases/conformance/types/literal/stringLiteralsAssertionsInEqualityComparisons02.ts(5,9): error TS2365: Operator '==' cannot be applied to types 'string' and 'number'.
tests/cases/conformance/types/literal/stringLiteralsAssertionsInEqualityComparisons02.ts(5,19): error TS2352: Type 'string' cannot be converted to type 'number'.


==== tests/cases/conformance/types/literal/stringLiteralsAssertionsInEqualityComparisons02.ts (3 errors) ====
==== tests/cases/conformance/types/literal/stringLiteralsAssertionsInEqualityComparisons02.ts (5 errors) ====
type EnhancedString = string & { enhancements: any };

var a = "foo" === "bar" as "baz";
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2365: Operator '===' cannot be applied to types '"foo"' and '"baz"'.
~~~~~~~~~~~~~~
!!! error TS2352: Type '"bar"' cannot be converted to type '"baz"'.
var b = "foo" !== ("bar" as "foo");
~~~~~~~~~~~~~~
!!! error TS2352: Type '"bar"' cannot be converted to type '"foo"'.
var c = "foo" == (<number>"bar");
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types 'string' and 'number'.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tests/cases/conformance/types/literal/stringLiteralsWithTypeAssertions01.ts(3,9): error TS2352: Type '"foo"' cannot be converted to type '"bar"'.
tests/cases/conformance/types/literal/stringLiteralsWithTypeAssertions01.ts(4,9): error TS2352: Type '"bar"' cannot be converted to type '"foo"'.
tests/cases/conformance/types/literal/stringLiteralsWithTypeAssertions01.ts(7,9): error TS2352: Type '"foo" | "bar"' cannot be converted to type '"baz"'.
Type '"bar"' is not comparable to type '"baz"'.
tests/cases/conformance/types/literal/stringLiteralsWithTypeAssertions01.ts(8,9): error TS2352: Type '"baz"' cannot be converted to type '"foo" | "bar"'.


==== tests/cases/conformance/types/literal/stringLiteralsWithTypeAssertions01.ts (4 errors) ====
let fooOrBar: "foo" | "bar";

let a = "foo" as "bar";
~~~~~~~~~~~~~~
!!! error TS2352: Type '"foo"' cannot be converted to type '"bar"'.
let b = "bar" as "foo";
~~~~~~~~~~~~~~
!!! error TS2352: Type '"bar"' cannot be converted to type '"foo"'.
let c = fooOrBar as "foo";
let d = fooOrBar as "bar";
let e = fooOrBar as "baz";
~~~~~~~~~~~~~~~~~
!!! error TS2352: Type '"foo" | "bar"' cannot be converted to type '"baz"'.
!!! error TS2352: Type '"bar"' is not comparable to type '"baz"'.
let f = "baz" as typeof fooOrBar;
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2352: Type '"baz"' cannot be converted to type '"foo" | "bar"'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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