diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3d836ce38d4ca..c3792c85dd647 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -81,7 +81,7 @@ namespace ts { symbolToString, getAugmentedPropertiesOfType, getRootSymbols, - getContextualType, + getContextualType: getApparentTypeOfContextualType, getFullyQualifiedName, getResolvedSignature, getConstantValue, @@ -1653,7 +1653,7 @@ namespace ts { writeAnonymousType(type, flags); } else if (type.flags & TypeFlags.StringLiteral) { - writer.writeStringLiteral((type).text); + writer.writeStringLiteral(`"${escapeString((type).text)}"`); } else { // Should never get here @@ -4401,12 +4401,13 @@ namespace ts { } function getStringLiteralType(node: StringLiteral): StringLiteralType { - if (hasProperty(stringLiteralTypes, node.text)) { - return stringLiteralTypes[node.text]; + const text = node.text; + if (hasProperty(stringLiteralTypes, text)) { + return stringLiteralTypes[text]; } - const type = stringLiteralTypes[node.text] = createType(TypeFlags.StringLiteral); - type.text = getTextOfNode(node); + const type = stringLiteralTypes[text] = createType(TypeFlags.StringLiteral); + type.text = text; return type; } @@ -5737,6 +5738,10 @@ namespace ts { return !!getPropertyOfType(type, "0"); } + function isStringLiteralType(type: Type) { + return type.flags & TypeFlags.StringLiteral; + } + /** * Check if a Type was written as a tuple type literal. * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. @@ -6961,7 +6966,7 @@ namespace ts { else if (operator === SyntaxKind.BarBarToken) { // When an || expression has a contextual type, the operands are contextually typed by that type. When an || // expression has no contextual type, the right operand is contextually typed by the type of the left operand. - let type = getContextualType(binaryExpression); + let type = getApparentTypeOfContextualType(binaryExpression); if (!type && node === binaryExpression.right) { type = checkExpression(binaryExpression.left); } @@ -7008,6 +7013,10 @@ namespace ts { return applyToContextualType(type, t => getIndexTypeOfStructuredType(t, kind)); } + function contextualTypeIsStringLiteralType(type: Type): boolean { + return !!(type.flags & TypeFlags.Union ? forEach((type).types, isStringLiteralType) : isStringLiteralType(type)); + } + // Return true if the given contextual type is a tuple-like type function contextualTypeIsTupleLikeType(type: Type): boolean { return !!(type.flags & TypeFlags.Union ? forEach((type).types, isTupleLikeType) : isTupleLikeType(type)); @@ -7033,7 +7042,7 @@ namespace ts { function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElement) { const objectLiteral = element.parent; - const type = getContextualType(objectLiteral); + const type = getApparentTypeOfContextualType(objectLiteral); if (type) { if (!hasDynamicName(element)) { // For a (non-symbol) computed property, there is no reason to look up the name @@ -7059,7 +7068,7 @@ namespace ts { // type of T. function getContextualTypeForElementExpression(node: Expression): Type { const arrayLiteral = node.parent; - const type = getContextualType(arrayLiteral); + const type = getApparentTypeOfContextualType(arrayLiteral); if (type) { const index = indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) @@ -7072,7 +7081,7 @@ namespace ts { // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type. function getContextualTypeForConditionalOperand(node: Expression): Type { const conditional = node.parent; - return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; + return node === conditional.whenTrue || node === conditional.whenFalse ? getApparentTypeOfContextualType(conditional) : undefined; } function getContextualTypeForJsxExpression(expr: JsxExpression | JsxSpreadAttribute): Type { @@ -7097,12 +7106,22 @@ namespace ts { // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily // be "pushed" onto a node using the contextualType property. - function getContextualType(node: Expression): Type { - const type = getContextualTypeWorker(node); + function getApparentTypeOfContextualType(node: Expression): Type { + const type = getContextualType(node); return type && getApparentType(type); } - function getContextualTypeWorker(node: Expression): Type { + /** + * Woah! Do you really want to use this function? + * + * Unless you're trying to get the *non-apparent* type for a value-literal type, + * you probably meant to use 'getApparentTypeOfContextualType'. + * Otherwise this is slightly less useful. + * + * @param node the expression whose contextual type will be returned. + * @returns the contextual type of an expression. + */ + function getContextualType(node: Expression): Type { if (isInsideWithStatementBody(node)) { // We cannot answer semantic questions within a with block, do not proceed any further return undefined; @@ -7141,7 +7160,7 @@ namespace ts { Debug.assert(parent.parent.kind === SyntaxKind.TemplateExpression); return getContextualTypeForSubstitutionExpression(parent.parent, node); case SyntaxKind.ParenthesizedExpression: - return getContextualType(parent); + return getApparentTypeOfContextualType(parent); case SyntaxKind.JsxExpression: case SyntaxKind.JsxSpreadAttribute: return getContextualTypeForJsxExpression(parent); @@ -7181,7 +7200,7 @@ namespace ts { Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node)); const type = isObjectLiteralMethod(node) ? getContextualTypeForObjectLiteralMethod(node) - : getContextualType(node); + : getApparentTypeOfContextualType(node); if (!type) { return undefined; } @@ -7311,7 +7330,7 @@ namespace ts { type.pattern = node; return type; } - const contextualType = getContextualType(node); + const contextualType = getApparentTypeOfContextualType(node); if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { const pattern = contextualType.pattern; // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting @@ -7403,7 +7422,7 @@ namespace ts { const propertiesTable: SymbolTable = {}; const propertiesArray: Symbol[] = []; - const contextualType = getContextualType(node); + const contextualType = getApparentTypeOfContextualType(node); const contextualTypeHasPattern = contextualType && contextualType.pattern && (contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression); let typeFlags: TypeFlags = 0; @@ -9404,7 +9423,12 @@ namespace ts { const targetType = getTypeFromTypeNode(node.type); if (produceDiagnostics && targetType !== unknownType) { const widenedType = getWidenedType(exprType); - if (!(isTypeAssignableTo(targetType, widenedType))) { + + // Permit 'number[] | "foo"' to be asserted to 'string'. + const bothAreStringLike = + someConstituentTypeHasKind(targetType, TypeFlags.StringLike) && + someConstituentTypeHasKind(widenedType, TypeFlags.StringLike); + if (!bothAreStringLike && !(isTypeAssignableTo(targetType, widenedType))) { checkTypeAssignableTo(exprType, targetType, node, Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } @@ -10234,6 +10258,10 @@ namespace ts { case SyntaxKind.ExclamationEqualsToken: case SyntaxKind.EqualsEqualsEqualsToken: case SyntaxKind.ExclamationEqualsEqualsToken: + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (someConstituentTypeHasKind(leftType, TypeFlags.StringLike) && someConstituentTypeHasKind(rightType, TypeFlags.StringLike)) { + return booleanType; + } if (!isTypeAssignableTo(leftType, rightType) && !isTypeAssignableTo(rightType, leftType)) { reportOperatorError(); } @@ -10372,6 +10400,15 @@ namespace ts { return getUnionType([type1, type2]); } + function checkStringLiteralExpression(node: StringLiteral): Type { + const contextualType = getContextualType(node); + if (contextualType && contextualTypeIsStringLiteralType(contextualType)) { + return getStringLiteralType(node); + } + + return stringType; + } + function checkTemplateExpression(node: TemplateExpression): Type { // We just want to check each expressions, but we are unconcerned with // the type of each expression, as any value may be coerced into a string. @@ -10431,7 +10468,7 @@ namespace ts { if (isInferentialContext(contextualMapper)) { const signature = getSingleCallSignature(type); if (signature && signature.typeParameters) { - const contextualType = getContextualType(node); + const contextualType = getApparentTypeOfContextualType(node); if (contextualType) { const contextualSignature = getSingleCallSignature(contextualType); if (contextualSignature && !contextualSignature.typeParameters) { @@ -10502,6 +10539,7 @@ namespace ts { case SyntaxKind.TemplateExpression: return checkTemplateExpression(node); case SyntaxKind.StringLiteral: + return checkStringLiteralExpression(node); case SyntaxKind.NoSubstitutionTemplateLiteral: return stringType; case SyntaxKind.RegularExpressionLiteral: @@ -12696,6 +12734,7 @@ namespace ts { let hasDuplicateDefaultClause = false; const expressionType = checkExpression(node.expression); + const expressionTypeIsStringLike = someConstituentTypeHasKind(expressionType, TypeFlags.StringLike); forEach(node.caseBlock.clauses, clause => { // Grammar check for duplicate default clauses, skip if we already report duplicate default clause if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) { @@ -12716,6 +12755,12 @@ namespace ts { // TypeScript 1.0 spec (April 2014):5.9 // In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression. const caseType = checkExpression(caseClause.expression); + + // Permit 'number[] | "foo"' to be asserted to 'string'. + if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, TypeFlags.StringLike)) { + return; + } + if (!isTypeAssignableTo(expressionType, caseType)) { // check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index ab06b12318b77..dfda8f446ca2e 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1979,9 +1979,7 @@ namespace ts { function parseParameterType(): TypeNode { if (parseOptional(SyntaxKind.ColonToken)) { - return token === SyntaxKind.StringLiteral - ? parseLiteralNode(/*internName*/ true) - : parseType(); + return parseType(); } return undefined; @@ -2369,6 +2367,8 @@ namespace ts { // If these are followed by a dot, then parse these out as a dotted type reference instead. const node = tryParse(parseKeywordAndNoDot); return node || parseTypeReferenceOrTypePredicate(); + case SyntaxKind.StringLiteral: + return parseLiteralNode(/*internName*/ true); case SyntaxKind.VoidKeyword: case SyntaxKind.ThisKeyword: return parseTokenNode(); @@ -2399,6 +2399,7 @@ namespace ts { case SyntaxKind.OpenBracketToken: case SyntaxKind.LessThanToken: case SyntaxKind.NewKeyword: + case SyntaxKind.StringLiteral: return true; case SyntaxKind.OpenParenToken: // Only consider '(' the start of a type if followed by ')', '...', an identifier, a modifier, @@ -5616,6 +5617,7 @@ namespace ts { return parseTokenNode(); } + // TODO (drosen): Parse string literal types in JSDoc as well. return parseJSDocTypeReference(); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f536a1e469307..d3cf86fed12c7 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -701,7 +701,7 @@ namespace ts { } // Note that a StringLiteral AST node is both an Expression and a TypeNode. The latter is - // because string literals can appear in the type annotation of a parameter node. + // because string literals can appear in type annotations as well. export interface StringLiteral extends LiteralExpression, TypeNode { _stringLiteralBrand: any; } diff --git a/tests/baselines/reference/callSignatureFunctionOverload.types b/tests/baselines/reference/callSignatureFunctionOverload.types index b9788e5b143a9..d5897c01494ca 100644 --- a/tests/baselines/reference/callSignatureFunctionOverload.types +++ b/tests/baselines/reference/callSignatureFunctionOverload.types @@ -1,33 +1,33 @@ === tests/cases/compiler/callSignatureFunctionOverload.ts === var foo: { ->foo : { (name: string): string; (name: 'order'): string; (name: 'content'): string; (name: 'done'): string; } +>foo : { (name: string): string; (name: "order"): string; (name: "content"): string; (name: "done"): string; } (name: string): string; >name : string (name: 'order'): string; ->name : 'order' +>name : "order" (name: 'content'): string; ->name : 'content' +>name : "content" (name: 'done'): string; ->name : 'done' +>name : "done" } var foo2: { ->foo2 : { (name: string): string; (name: 'order'): string; (name: 'order'): string; (name: 'done'): string; } +>foo2 : { (name: string): string; (name: "order"): string; (name: "order"): string; (name: "done"): string; } (name: string): string; >name : string (name: 'order'): string; ->name : 'order' +>name : "order" (name: 'order'): string; ->name : 'order' +>name : "order" (name: 'done'): string; ->name : 'done' +>name : "done" } diff --git a/tests/baselines/reference/constantOverloadFunction.types b/tests/baselines/reference/constantOverloadFunction.types index d643d3a726d9a..d7deaaae46d79 100644 --- a/tests/baselines/reference/constantOverloadFunction.types +++ b/tests/baselines/reference/constantOverloadFunction.types @@ -19,27 +19,27 @@ class Derived3 extends Base { biz() { } } >biz : () => void function foo(tagName: 'canvas'): Derived1; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } ->tagName : 'canvas' +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } +>tagName : "canvas" >Derived1 : Derived1 function foo(tagName: 'div'): Derived2; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } ->tagName : 'div' +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } +>tagName : "div" >Derived2 : Derived2 function foo(tagName: 'span'): Derived3; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } ->tagName : 'span' +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } +>tagName : "span" >Derived3 : Derived3 function foo(tagName: string): Base; ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } >tagName : string >Base : Base function foo(tagName: any): Base { ->foo : { (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; (tagName: string): Base; } +>foo : { (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; (tagName: string): Base; } >tagName : any >Base : Base diff --git a/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types b/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types index dc8b9b465d81f..563cb58a755e0 100644 --- a/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types +++ b/tests/baselines/reference/inheritedOverloadedSpecializedSignatures.types @@ -11,7 +11,7 @@ interface B extends A { >A : A (key:'foo'):string; ->key : 'foo' +>key : "foo" } var b:B; @@ -24,7 +24,7 @@ b('foo').charAt(0); >b('foo').charAt : (pos: number) => string >b('foo') : string >b : B ->'foo' : string +>'foo' : "foo" >charAt : (pos: number) => string >0 : number @@ -32,7 +32,7 @@ interface A { >A : A (x: 'A1'): string; ->x : 'A1' +>x : "A1" (x: string): void; >x : string @@ -43,21 +43,21 @@ interface B extends A { >A : A (x: 'B1'): number; ->x : 'B1' +>x : "B1" } interface A { >A : A (x: 'A2'): boolean; ->x : 'A2' +>x : "A2" } interface B { >B : B (x: 'B2'): string[]; ->x : 'B2' +>x : "B2" } interface C1 extends B { @@ -65,7 +65,7 @@ interface C1 extends B { >B : B (x: 'C1'): number[]; ->x : 'C1' +>x : "C1" } interface C2 extends B { @@ -73,7 +73,7 @@ interface C2 extends B { >B : B (x: 'C2'): boolean[]; ->x : 'C2' +>x : "C2" } interface C extends C1, C2 { @@ -82,7 +82,7 @@ interface C extends C1, C2 { >C2 : C2 (x: 'C'): string; ->x : 'C' +>x : "C" } var c: C; @@ -94,25 +94,25 @@ var x1: string[] = c('B2'); >x1 : string[] >c('B2') : string[] >c : C ->'B2' : string +>'B2' : "B2" var x2: number = c('B1'); >x2 : number >c('B1') : number >c : C ->'B1' : string +>'B1' : "B1" var x3: boolean = c('A2'); >x3 : boolean >c('A2') : boolean >c : C ->'A2' : string +>'A2' : "A2" var x4: string = c('A1'); >x4 : string >c('A1') : string >c : C ->'A1' : string +>'A1' : "A1" var x5: void = c('A0'); >x5 : void @@ -124,19 +124,19 @@ var x6: number[] = c('C1'); >x6 : number[] >c('C1') : number[] >c : C ->'C1' : string +>'C1' : "C1" var x7: boolean[] = c('C2'); >x7 : boolean[] >c('C2') : boolean[] >c : C ->'C2' : string +>'C2' : "C2" var x8: string = c('C'); >x8 : string >c('C') : string >c : C ->'C' : string +>'C' : "C" var x9: void = c('generic'); >x9 : void diff --git a/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types b/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types index 971b5e56907fa..5288d9f15a434 100644 --- a/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types +++ b/tests/baselines/reference/interfaceWithSpecializedCallAndConstructSignatures.types @@ -3,13 +3,13 @@ interface Foo { >Foo : Foo (x: 'a'): number; ->x : 'a' +>x : "a" (x: string): any; >x : string new (x: 'a'): any; ->x : 'a' +>x : "a" new (x: string): Object; >x : string @@ -24,7 +24,7 @@ var r = f('a'); >r : number >f('a') : number >f : Foo ->'a' : string +>'a' : "a" var r2 = f('A'); >r2 : any @@ -36,7 +36,7 @@ var r3 = new f('a'); >r3 : any >new f('a') : any >f : Foo ->'a' : string +>'a' : "a" var r4 = new f('A'); >r4 : Object diff --git a/tests/baselines/reference/memberFunctionsWithPublicOverloads.types b/tests/baselines/reference/memberFunctionsWithPublicOverloads.types index 7cbc2aca1ffb8..bcd937e9cfc2b 100644 --- a/tests/baselines/reference/memberFunctionsWithPublicOverloads.types +++ b/tests/baselines/reference/memberFunctionsWithPublicOverloads.types @@ -17,20 +17,20 @@ class C { >y : any public bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } +>x : "hi" public bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : string public bar(x: number, y: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : number >y : string public bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : any >y : any @@ -49,20 +49,20 @@ class C { >y : any public static bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } +>x : "hi" public static bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : string public static bar(x: number, y: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : number >y : string public static bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : any >y : any } @@ -88,22 +88,22 @@ class D { >y : any public bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } +>x : "hi" public bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } >x : string public bar(x: T, y: T); ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } >x : T >T : T >y : T >T : T public bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: T, y: T): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: T, y: T): any; } >x : any >y : any @@ -122,20 +122,20 @@ class D { >y : any public static bar(x: 'hi'); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } ->x : 'hi' +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } +>x : "hi" public static bar(x: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : string public static bar(x: number, y: string); ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : number >y : string public static bar(x: any, y?: any) { } ->bar : { (x: 'hi'): any; (x: string): any; (x: number, y: string): any; } +>bar : { (x: "hi"): any; (x: string): any; (x: number, y: string): any; } >x : any >y : any diff --git a/tests/baselines/reference/overloadOnConstConstraintChecks1.types b/tests/baselines/reference/overloadOnConstConstraintChecks1.types index cc561eb6e21c6..012774b3cc9eb 100644 --- a/tests/baselines/reference/overloadOnConstConstraintChecks1.types +++ b/tests/baselines/reference/overloadOnConstConstraintChecks1.types @@ -22,23 +22,23 @@ interface MyDoc { // Document >MyDoc : MyDoc createElement(tagName: string): Base; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } >tagName : string >Base : Base createElement(tagName: 'canvas'): Derived1; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'canvas' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "canvas" >Derived1 : Derived1 createElement(tagName: 'div'): Derived2; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'div' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "div" >Derived2 : Derived2 createElement(tagName: 'span'): Derived3; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'span' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "span" >Derived3 : Derived3 // + 100 more @@ -49,27 +49,27 @@ class D implements MyDoc { >MyDoc : MyDoc createElement(tagName:string): Base; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } >tagName : string >Base : Base createElement(tagName: 'canvas'): Derived1; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'canvas' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "canvas" >Derived1 : Derived1 createElement(tagName: 'div'): Derived2; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'div' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "div" >Derived2 : Derived2 createElement(tagName: 'span'): Derived3; ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } ->tagName : 'span' +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } +>tagName : "span" >Derived3 : Derived3 createElement(tagName:any): Base { ->createElement : { (tagName: string): Base; (tagName: 'canvas'): Derived1; (tagName: 'div'): Derived2; (tagName: 'span'): Derived3; } +>createElement : { (tagName: string): Base; (tagName: "canvas"): Derived1; (tagName: "div"): Derived2; (tagName: "span"): Derived3; } >tagName : any >Base : Base diff --git a/tests/baselines/reference/overloadOnConstConstraintChecks2.types b/tests/baselines/reference/overloadOnConstConstraintChecks2.types index 36fa89dec4a5a..6102c282a60a3 100644 --- a/tests/baselines/reference/overloadOnConstConstraintChecks2.types +++ b/tests/baselines/reference/overloadOnConstConstraintChecks2.types @@ -14,22 +14,22 @@ class C extends A { >foo : () => void } function foo(name: 'hi'): B; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'hi' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "hi" >B : B function foo(name: 'bye'): C; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'bye' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "bye" >C : C function foo(name: string): A; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : string >A : A function foo(name: any): A { ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : any >A : A diff --git a/tests/baselines/reference/overloadOnConstConstraintChecks3.types b/tests/baselines/reference/overloadOnConstConstraintChecks3.types index 94a03bb768070..0348d3d4b79b8 100644 --- a/tests/baselines/reference/overloadOnConstConstraintChecks3.types +++ b/tests/baselines/reference/overloadOnConstConstraintChecks3.types @@ -16,22 +16,22 @@ class C extends A { >foo : () => void } function foo(name: 'hi'): B; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'hi' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "hi" >B : B function foo(name: 'bye'): C; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } ->name : 'bye' +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } +>name : "bye" >C : C function foo(name: string): A; ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : string >A : A function foo(name: any): A { ->foo : { (name: 'hi'): B; (name: 'bye'): C; (name: string): A; } +>foo : { (name: "hi"): B; (name: "bye"): C; (name: string): A; } >name : any >A : A diff --git a/tests/baselines/reference/overloadOnConstInheritance1.types b/tests/baselines/reference/overloadOnConstInheritance1.types index 96f8df3dffdf9..4721c18e55c8a 100644 --- a/tests/baselines/reference/overloadOnConstInheritance1.types +++ b/tests/baselines/reference/overloadOnConstInheritance1.types @@ -3,23 +3,23 @@ interface Base { >Base : Base addEventListener(x: string): any; ->addEventListener : { (x: string): any; (x: 'foo'): string; } +>addEventListener : { (x: string): any; (x: "foo"): string; } >x : string addEventListener(x: 'foo'): string; ->addEventListener : { (x: string): any; (x: 'foo'): string; } ->x : 'foo' +>addEventListener : { (x: string): any; (x: "foo"): string; } +>x : "foo" } interface Deriver extends Base { >Deriver : Deriver >Base : Base addEventListener(x: string): any; ->addEventListener : { (x: string): any; (x: 'bar'): string; } +>addEventListener : { (x: string): any; (x: "bar"): string; } >x : string addEventListener(x: 'bar'): string; ->addEventListener : { (x: string): any; (x: 'bar'): string; } ->x : 'bar' +>addEventListener : { (x: string): any; (x: "bar"): string; } +>x : "bar" } diff --git a/tests/baselines/reference/overloadOnConstInheritance2.errors.txt b/tests/baselines/reference/overloadOnConstInheritance2.errors.txt index 159af64a13ae2..4870106f4745b 100644 --- a/tests/baselines/reference/overloadOnConstInheritance2.errors.txt +++ b/tests/baselines/reference/overloadOnConstInheritance2.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadOnConstInheritance2.ts(5,11): error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. Types of property 'addEventListener' are incompatible. - Type '(x: 'bar') => string' is not assignable to type '{ (x: string): any; (x: 'foo'): string; }'. + Type '(x: "bar") => string' is not assignable to type '{ (x: string): any; (x: "foo"): string; }'. tests/cases/compiler/overloadOnConstInheritance2.ts(6,5): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. @@ -13,7 +13,7 @@ tests/cases/compiler/overloadOnConstInheritance2.ts(6,5): error TS2382: Speciali ~~~~~~~ !!! error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. !!! error TS2430: Types of property 'addEventListener' are incompatible. -!!! error TS2430: Type '(x: 'bar') => string' is not assignable to type '{ (x: string): any; (x: 'foo'): string; }'. +!!! error TS2430: Type '(x: "bar") => string' is not assignable to type '{ (x: string): any; (x: "foo"): string; }'. addEventListener(x: 'bar'): string; // shouldn't need to redeclare the string overload ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. diff --git a/tests/baselines/reference/overloadOnConstInheritance3.errors.txt b/tests/baselines/reference/overloadOnConstInheritance3.errors.txt index e0fff7b1ec73c..b338f9795d79c 100644 --- a/tests/baselines/reference/overloadOnConstInheritance3.errors.txt +++ b/tests/baselines/reference/overloadOnConstInheritance3.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadOnConstInheritance3.ts(4,11): error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. Types of property 'addEventListener' are incompatible. - Type '{ (x: 'bar'): string; (x: 'foo'): string; }' is not assignable to type '(x: string) => any'. + Type '{ (x: "bar"): string; (x: "foo"): string; }' is not assignable to type '(x: string) => any'. tests/cases/compiler/overloadOnConstInheritance3.ts(6,5): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/compiler/overloadOnConstInheritance3.ts(7,5): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. @@ -13,7 +13,7 @@ tests/cases/compiler/overloadOnConstInheritance3.ts(7,5): error TS2382: Speciali ~~~~~~~ !!! error TS2430: Interface 'Deriver' incorrectly extends interface 'Base'. !!! error TS2430: Types of property 'addEventListener' are incompatible. -!!! error TS2430: Type '{ (x: 'bar'): string; (x: 'foo'): string; }' is not assignable to type '(x: string) => any'. +!!! error TS2430: Type '{ (x: "bar"): string; (x: "foo"): string; }' is not assignable to type '(x: string) => any'. // shouldn't need to redeclare the string overload addEventListener(x: 'bar'): string; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt b/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt index 1676a2fb426a0..4fea7cc122795 100644 --- a/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt +++ b/tests/baselines/reference/overloadOnConstantsInvalidOverload1.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(6,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(7,10): error TS2381: A signature with an implementation cannot use a string literal type. -tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(11,5): error TS2345: Argument of type 'string' is not assignable to parameter of type '"SPAN"'. +tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(11,5): error TS2345: Argument of type '"HI"' is not assignable to parameter of type '"SPAN"'. ==== tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts (3 errors) ==== @@ -20,4 +20,4 @@ tests/cases/compiler/overloadOnConstantsInvalidOverload1.ts(11,5): error TS2345: foo("HI"); ~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"SPAN"'. \ No newline at end of file +!!! error TS2345: Argument of type '"HI"' is not assignable to parameter of type '"SPAN"'. \ No newline at end of file diff --git a/tests/baselines/reference/overloadingOnConstants2.errors.txt b/tests/baselines/reference/overloadingOnConstants2.errors.txt index c6d6352357681..b448deaadc75b 100644 --- a/tests/baselines/reference/overloadingOnConstants2.errors.txt +++ b/tests/baselines/reference/overloadingOnConstants2.errors.txt @@ -1,6 +1,6 @@ tests/cases/compiler/overloadingOnConstants2.ts(8,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/compiler/overloadingOnConstants2.ts(9,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. -tests/cases/compiler/overloadingOnConstants2.ts(15,13): error TS2345: Argument of type 'string' is not assignable to parameter of type '"bye"'. +tests/cases/compiler/overloadingOnConstants2.ts(15,13): error TS2345: Argument of type '"um"' is not assignable to parameter of type '"bye"'. tests/cases/compiler/overloadingOnConstants2.ts(19,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. @@ -25,7 +25,7 @@ tests/cases/compiler/overloadingOnConstants2.ts(19,10): error TS2382: Specialize var b: E = foo("bye", []); // E var c = foo("um", []); // error ~~~~ -!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"bye"'. +!!! error TS2345: Argument of type '"um"' is not assignable to parameter of type '"bye"'. //function bar(x: "hi", items: string[]): D; diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.errors.txt b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.errors.txt deleted file mode 100644 index 707e68c2c4542..0000000000000 --- a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.errors.txt +++ /dev/null @@ -1,34 +0,0 @@ -tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts(12,21): error TS1110: Type expected. - - -==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts (1 errors) ==== - // Interface - interface IPoint { - getDist(): number; - } - - // Module - module Shapes { - - // Class - export class Point implements IPoint { - - public con: "hello"; - ~~~~~~~ -!!! error TS1110: Type expected. - // Constructor - constructor (public x: number, public y: number) { } - - // Instance member - getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); } - - // Static member - static origin = new Point(0, 0); - } - - } - - // Local variables - var p: IPoint = new Shapes.Point(3, 4); - var dist = p.getDist(); - \ No newline at end of file diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js index ae2509c3f4929..8055cd9d4947b 100644 --- a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.js @@ -38,7 +38,6 @@ var Shapes; function Point(x, y) { this.x = x; this.y = y; - this.con = "hello"; } // Instance member Point.prototype.getDist = function () { return Math.sqrt(this.x * this.x + this.y * this.y); }; diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols new file mode 100644 index 0000000000000..7b8be05f923f8 --- /dev/null +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.symbols @@ -0,0 +1,67 @@ +=== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts === +// Interface +interface IPoint { +>IPoint : Symbol(IPoint, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 0, 0)) + + getDist(): number; +>getDist : Symbol(getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 1, 18)) +} + +// Module +module Shapes { +>Shapes : Symbol(Shapes, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 3, 1)) + + // Class + export class Point implements IPoint { +>Point : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>IPoint : Symbol(IPoint, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 0, 0)) + + public con: "hello"; +>con : Symbol(con, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 9, 42)) + + // Constructor + constructor (public x: number, public y: number) { } +>x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) + + // Instance member + getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); } +>getDist : Symbol(getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 60)) +>Math.sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, --, --)) +>this.x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this.x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>x : Symbol(x, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 21)) +>this.y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) +>this.y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) +>this : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>y : Symbol(y, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 13, 38)) + + // Static member + static origin = new Point(0, 0); +>origin : Symbol(Point.origin, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 16, 74)) +>Point : Symbol(Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) + } + +} + +// Local variables +var p: IPoint = new Shapes.Point(3, 4); +>p : Symbol(p, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 25, 3)) +>IPoint : Symbol(IPoint, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 0, 0)) +>Shapes.Point : Symbol(Shapes.Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) +>Shapes : Symbol(Shapes, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 3, 1)) +>Point : Symbol(Shapes.Point, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 6, 15)) + +var dist = p.getDist(); +>dist : Symbol(dist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 26, 3)) +>p.getDist : Symbol(IPoint.getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 1, 18)) +>p : Symbol(p, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 25, 3)) +>getDist : Symbol(IPoint.getDist, Decl(parserErrorRecovery_IncompleteMemberVariable1.ts, 1, 18)) + diff --git a/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types new file mode 100644 index 0000000000000..b38bbbfe64d4d --- /dev/null +++ b/tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable1.types @@ -0,0 +1,78 @@ +=== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IncompleteMemberVariables/parserErrorRecovery_IncompleteMemberVariable1.ts === +// Interface +interface IPoint { +>IPoint : IPoint + + getDist(): number; +>getDist : () => number +} + +// Module +module Shapes { +>Shapes : typeof Shapes + + // Class + export class Point implements IPoint { +>Point : Point +>IPoint : IPoint + + public con: "hello"; +>con : "hello" + + // Constructor + constructor (public x: number, public y: number) { } +>x : number +>y : number + + // Instance member + getDist() { return Math.sqrt(this.x * this.x + this.y * this.y); } +>getDist : () => number +>Math.sqrt(this.x * this.x + this.y * this.y) : number +>Math.sqrt : (x: number) => number +>Math : Math +>sqrt : (x: number) => number +>this.x * this.x + this.y * this.y : number +>this.x * this.x : number +>this.x : number +>this : this +>x : number +>this.x : number +>this : this +>x : number +>this.y * this.y : number +>this.y : number +>this : this +>y : number +>this.y : number +>this : this +>y : number + + // Static member + static origin = new Point(0, 0); +>origin : Point +>new Point(0, 0) : Point +>Point : typeof Point +>0 : number +>0 : number + } + +} + +// Local variables +var p: IPoint = new Shapes.Point(3, 4); +>p : IPoint +>IPoint : IPoint +>new Shapes.Point(3, 4) : Shapes.Point +>Shapes.Point : typeof Shapes.Point +>Shapes : typeof Shapes +>Point : typeof Shapes.Point +>3 : number +>4 : number + +var dist = p.getDist(); +>dist : number +>p.getDist() : number +>p.getDist : () => number +>p : IPoint +>getDist : () => number + diff --git a/tests/baselines/reference/primtiveTypesAreIdentical.types b/tests/baselines/reference/primtiveTypesAreIdentical.types index bbff4623f0b88..0b8e7dadf7dc3 100644 --- a/tests/baselines/reference/primtiveTypesAreIdentical.types +++ b/tests/baselines/reference/primtiveTypesAreIdentical.types @@ -50,19 +50,19 @@ function foo4(x: any) { } >x : any function foo5(x: 'a'); ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } +>x : "a" function foo5(x: 'a'); ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } +>x : "a" function foo5(x: string); ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } >x : string function foo5(x: any) { } ->foo5 : { (x: 'a'): any; (x: 'a'): any; (x: string): any; } +>foo5 : { (x: "a"): any; (x: "a"): any; (x: string): any; } >x : any enum E { A } diff --git a/tests/baselines/reference/specializedSignatureInInterface.types b/tests/baselines/reference/specializedSignatureInInterface.types index c8f76f4cb9b09..b9621af7b8be0 100644 --- a/tests/baselines/reference/specializedSignatureInInterface.types +++ b/tests/baselines/reference/specializedSignatureInInterface.types @@ -11,8 +11,8 @@ interface B extends A { >A : A (key:'foo'):string; ->key : 'foo' +>key : "foo" (key:'bar'):string; ->key : 'bar' +>key : "bar" } diff --git a/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types b/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types index cb86966d65a0b..b27a935ced687 100644 --- a/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types +++ b/tests/baselines/reference/specializedSignatureIsSubtypeOfNonSpecializedSignature.types @@ -3,30 +3,30 @@ // All the below should not be errors function foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; } +>x : "a" function foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : string function foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : any class C { >C : C foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : string foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; } +>foo : { (x: "a"): any; (x: string): any; } >x : any } @@ -35,20 +35,20 @@ class C2 { >T : T foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : any } @@ -58,20 +58,20 @@ class C3 { >String : String foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T foo(x: any) { } ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : any } @@ -79,7 +79,7 @@ interface I { >I : I (x: 'a'); ->x : 'a' +>x : "a" (x: number); >x : number @@ -88,15 +88,15 @@ interface I { >x : string foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: number): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: number): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: number): any; } +>foo : { (x: "a"): any; (x: string): any; (x: number): any; } >x : string foo(x: number); ->foo : { (x: 'a'): any; (x: string): any; (x: number): any; } +>foo : { (x: "a"): any; (x: string): any; (x: number): any; } >x : number } @@ -105,7 +105,7 @@ interface I2 { >T : T (x: 'a'); ->x : 'a' +>x : "a" (x: T); >x : T @@ -115,15 +115,15 @@ interface I2 { >x : string foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T } @@ -134,7 +134,7 @@ interface I3 { >String : String (x: 'a'); ->x : 'a' +>x : "a" (x: string); >x : string @@ -144,49 +144,49 @@ interface I3 { >T : T foo(x: 'a'); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } ->x : 'a' +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } +>x : "a" foo(x: string); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : string foo(x: T); ->foo : { (x: 'a'): any; (x: string): any; (x: T): any; } +>foo : { (x: "a"): any; (x: string): any; (x: T): any; } >x : T >T : T } var a: { ->a : { (x: string): any; (x: 'a'): any; (x: number): any; foo(x: string): any; foo(x: 'a'): any; foo(x: number): any; } +>a : { (x: string): any; (x: "a"): any; (x: number): any; foo(x: string): any; foo(x: "a"): any; foo(x: number): any; } (x: string); >x : string (x: 'a'); ->x : 'a' +>x : "a" (x: number); >x : number foo(x: string); ->foo : { (x: string): any; (x: 'a'): any; (x: number): any; } +>foo : { (x: string): any; (x: "a"): any; (x: number): any; } >x : string foo(x: 'a'); ->foo : { (x: string): any; (x: 'a'): any; (x: number): any; } ->x : 'a' +>foo : { (x: string): any; (x: "a"): any; (x: number): any; } +>x : "a" foo(x: number); ->foo : { (x: string): any; (x: 'a'): any; (x: number): any; } +>foo : { (x: string): any; (x: "a"): any; (x: number): any; } >x : number } var a2: { ->a2 : { (x: 'a'): any; (x: string): any; (x: T): any; foo(x: string): any; foo(x: 'a'): any; foo(x: T): any; } +>a2 : { (x: "a"): any; (x: string): any; (x: T): any; foo(x: string): any; foo(x: "a"): any; foo(x: T): any; } (x: 'a'); ->x : 'a' +>x : "a" (x: string); >x : string @@ -197,25 +197,25 @@ var a2: { >T : T foo(x: string); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >x : string foo(x: 'a'); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } ->x : 'a' +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } +>x : "a" foo(x: T); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >T : T >x : T >T : T } var a3: { ->a3 : { (x: 'a'): any; (x: T): any; (x: string): any; foo(x: string): any; foo(x: 'a'): any; foo(x: T): any; } +>a3 : { (x: "a"): any; (x: T): any; (x: string): any; foo(x: string): any; foo(x: "a"): any; foo(x: T): any; } (x: 'a'); ->x : 'a' +>x : "a" (x: T); >T : T @@ -226,15 +226,15 @@ var a3: { >x : string foo(x: string); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >x : string foo(x: 'a'); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } ->x : 'a' +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } +>x : "a" foo(x: T); ->foo : { (x: string): any; (x: 'a'): any; (x: T): any; } +>foo : { (x: string): any; (x: "a"): any; (x: T): any; } >T : T >String : String >x : T diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.js b/tests/baselines/reference/stringLiteralCheckedInIf01.js new file mode 100644 index 0000000000000..161c39b96efa4 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.js @@ -0,0 +1,29 @@ +//// [stringLiteralCheckedInIf01.ts] + +type S = "a" | "b"; +type T = S[] | S; + +function f(foo: T) { + if (foo === "a") { + return foo; + } + else if (foo === "b") { + return foo; + } + else { + return (foo as S[])[0]; + } +} + +//// [stringLiteralCheckedInIf01.js] +function f(foo) { + if (foo === "a") { + return foo; + } + else if (foo === "b") { + return foo; + } + else { + return foo[0]; + } +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.symbols b/tests/baselines/reference/stringLiteralCheckedInIf01.symbols new file mode 100644 index 0000000000000..1b4d5e6b07125 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.symbols @@ -0,0 +1,33 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralCheckedInIf01.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) + +function f(foo: T) { +>f : Symbol(f, Decl(stringLiteralCheckedInIf01.ts, 2, 17)) +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) +>T : Symbol(T, Decl(stringLiteralCheckedInIf01.ts, 1, 19)) + + if (foo === "a") { +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + + return foo; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + } + else if (foo === "b") { +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + + return foo; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) + } + else { + return (foo as S[])[0]; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf01.ts, 4, 11)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf01.ts, 0, 0)) + } +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf01.types b/tests/baselines/reference/stringLiteralCheckedInIf01.types new file mode 100644 index 0000000000000..75b7eadbc19b4 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf01.types @@ -0,0 +1,41 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +function f(foo: T) { +>f : (foo: ("a" | "b")[] | "a" | "b") => ("a" | "b")[] | "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + + if (foo === "a") { +>foo === "a" : boolean +>foo : ("a" | "b")[] | "a" | "b" +>"a" : string + + return foo; +>foo : ("a" | "b")[] | "a" | "b" + } + else if (foo === "b") { +>foo === "b" : boolean +>foo : ("a" | "b")[] | "a" | "b" +>"b" : string + + return foo; +>foo : ("a" | "b")[] | "a" | "b" + } + else { + return (foo as S[])[0]; +>(foo as S[])[0] : "a" | "b" +>(foo as S[]) : ("a" | "b")[] +>foo as S[] : ("a" | "b")[] +>foo : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>0 : number + } +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.js b/tests/baselines/reference/stringLiteralCheckedInIf02.js new file mode 100644 index 0000000000000..22bd6359f6187 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.js @@ -0,0 +1,30 @@ +//// [stringLiteralCheckedInIf02.ts] + +type S = "a" | "b"; +type T = S[] | S; + +function isS(t: T): t is S { + return t === "a" || t === "b"; +} + +function f(foo: T) { + if (isS(foo)) { + return foo; + } + else { + return foo[0]; + } +} + +//// [stringLiteralCheckedInIf02.js] +function isS(t) { + return t === "a" || t === "b"; +} +function f(foo) { + if (isS(foo)) { + return foo; + } + else { + return foo[0]; + } +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.symbols b/tests/baselines/reference/stringLiteralCheckedInIf02.symbols new file mode 100644 index 0000000000000..629f990b91260 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.symbols @@ -0,0 +1,39 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralCheckedInIf02.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) + +function isS(t: T): t is S { +>isS : Symbol(isS, Decl(stringLiteralCheckedInIf02.ts, 2, 17)) +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +>T : Symbol(T, Decl(stringLiteralCheckedInIf02.ts, 1, 19)) +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +>S : Symbol(S, Decl(stringLiteralCheckedInIf02.ts, 0, 0)) + + return t === "a" || t === "b"; +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +>t : Symbol(t, Decl(stringLiteralCheckedInIf02.ts, 4, 13)) +} + +function f(foo: T) { +>f : Symbol(f, Decl(stringLiteralCheckedInIf02.ts, 6, 1)) +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) +>T : Symbol(T, Decl(stringLiteralCheckedInIf02.ts, 1, 19)) + + if (isS(foo)) { +>isS : Symbol(isS, Decl(stringLiteralCheckedInIf02.ts, 2, 17)) +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) + + return foo; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) + } + else { + return foo[0]; +>foo : Symbol(foo, Decl(stringLiteralCheckedInIf02.ts, 8, 11)) + } +} diff --git a/tests/baselines/reference/stringLiteralCheckedInIf02.types b/tests/baselines/reference/stringLiteralCheckedInIf02.types new file mode 100644 index 0000000000000..79f4c6a223a71 --- /dev/null +++ b/tests/baselines/reference/stringLiteralCheckedInIf02.types @@ -0,0 +1,47 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +function isS(t: T): t is S { +>isS : (t: ("a" | "b")[] | "a" | "b") => t is "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" +>t : any +>S : "a" | "b" + + return t === "a" || t === "b"; +>t === "a" || t === "b" : boolean +>t === "a" : boolean +>t : ("a" | "b")[] | "a" | "b" +>"a" : string +>t === "b" : boolean +>t : ("a" | "b")[] | "a" | "b" +>"b" : string +} + +function f(foo: T) { +>f : (foo: ("a" | "b")[] | "a" | "b") => "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + + if (isS(foo)) { +>isS(foo) : boolean +>isS : (t: ("a" | "b")[] | "a" | "b") => t is "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" + + return foo; +>foo : "a" | "b" + } + else { + return foo[0]; +>foo[0] : "a" | "b" +>foo : ("a" | "b")[] +>0 : number + } +} diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.js b/tests/baselines/reference/stringLiteralMatchedInSwitch01.js new file mode 100644 index 0000000000000..b9b24cf0c66b3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralMatchedInSwitch01.js @@ -0,0 +1,25 @@ +//// [stringLiteralMatchedInSwitch01.ts] + +type S = "a" | "b"; +type T = S[] | S; + +var foo: T; +switch (foo) { + case "a": + case "b": + break; + default: + foo = (foo as S[])[0]; + break; +} + +//// [stringLiteralMatchedInSwitch01.js] +var foo; +switch (foo) { + case "a": + case "b": + break; + default: + foo = foo[0]; + break; +} diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.symbols b/tests/baselines/reference/stringLiteralMatchedInSwitch01.symbols new file mode 100644 index 0000000000000..0323371c3070d --- /dev/null +++ b/tests/baselines/reference/stringLiteralMatchedInSwitch01.symbols @@ -0,0 +1,28 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralMatchedInSwitch01.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) + +var foo: T; +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralMatchedInSwitch01.ts, 1, 19)) + +switch (foo) { +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) + + case "a": + case "b": + break; + default: + foo = (foo as S[])[0]; +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) +>foo : Symbol(foo, Decl(stringLiteralMatchedInSwitch01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralMatchedInSwitch01.ts, 0, 0)) + + break; +} diff --git a/tests/baselines/reference/stringLiteralMatchedInSwitch01.types b/tests/baselines/reference/stringLiteralMatchedInSwitch01.types new file mode 100644 index 0000000000000..cfbb77e07e0a3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralMatchedInSwitch01.types @@ -0,0 +1,37 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +var foo: T; +>foo : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + +switch (foo) { +>foo : ("a" | "b")[] | "a" | "b" + + case "a": +>"a" : string + + case "b": +>"b" : string + + break; + default: + foo = (foo as S[])[0]; +>foo = (foo as S[])[0] : "a" | "b" +>foo : ("a" | "b")[] | "a" | "b" +>(foo as S[])[0] : "a" | "b" +>(foo as S[]) : ("a" | "b")[] +>foo as S[] : ("a" | "b")[] +>foo : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>0 : number + + break; +} diff --git a/tests/baselines/reference/stringLiteralType.errors.txt b/tests/baselines/reference/stringLiteralType.errors.txt deleted file mode 100644 index 275db139cf7b3..0000000000000 --- a/tests/baselines/reference/stringLiteralType.errors.txt +++ /dev/null @@ -1,12 +0,0 @@ -tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts(1,8): error TS1110: Type expected. - - -==== tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts (1 errors) ==== - var x: 'hi'; - ~~~~ -!!! error TS1110: Type expected. - - function f(x: 'hi'); - function f(x: string); - function f(x: any) { - } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralType.js b/tests/baselines/reference/stringLiteralType.js index 35f35f1cbef78..a6926453c47b0 100644 --- a/tests/baselines/reference/stringLiteralType.js +++ b/tests/baselines/reference/stringLiteralType.js @@ -7,6 +7,6 @@ function f(x: any) { } //// [stringLiteralType.js] -var x = 'hi'; +var x; function f(x) { } diff --git a/tests/baselines/reference/stringLiteralType.symbols b/tests/baselines/reference/stringLiteralType.symbols new file mode 100644 index 0000000000000..687d305244218 --- /dev/null +++ b/tests/baselines/reference/stringLiteralType.symbols @@ -0,0 +1,16 @@ +=== tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts === +var x: 'hi'; +>x : Symbol(x, Decl(stringLiteralType.ts, 0, 3)) + +function f(x: 'hi'); +>f : Symbol(f, Decl(stringLiteralType.ts, 0, 12), Decl(stringLiteralType.ts, 2, 20), Decl(stringLiteralType.ts, 3, 22)) +>x : Symbol(x, Decl(stringLiteralType.ts, 2, 11)) + +function f(x: string); +>f : Symbol(f, Decl(stringLiteralType.ts, 0, 12), Decl(stringLiteralType.ts, 2, 20), Decl(stringLiteralType.ts, 3, 22)) +>x : Symbol(x, Decl(stringLiteralType.ts, 3, 11)) + +function f(x: any) { +>f : Symbol(f, Decl(stringLiteralType.ts, 0, 12), Decl(stringLiteralType.ts, 2, 20), Decl(stringLiteralType.ts, 3, 22)) +>x : Symbol(x, Decl(stringLiteralType.ts, 4, 11)) +} diff --git a/tests/baselines/reference/stringLiteralType.types b/tests/baselines/reference/stringLiteralType.types new file mode 100644 index 0000000000000..affa995c2f30f --- /dev/null +++ b/tests/baselines/reference/stringLiteralType.types @@ -0,0 +1,16 @@ +=== tests/cases/conformance/types/primitives/stringLiteral/stringLiteralType.ts === +var x: 'hi'; +>x : "hi" + +function f(x: 'hi'); +>f : { (x: "hi"): any; (x: string): any; } +>x : "hi" + +function f(x: string); +>f : { (x: "hi"): any; (x: string): any; } +>x : string + +function f(x: any) { +>f : { (x: "hi"): any; (x: string): any; } +>x : any +} diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.js b/tests/baselines/reference/stringLiteralTypeAssertion01.js new file mode 100644 index 0000000000000..7877004cf01ab --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypeAssertion01.js @@ -0,0 +1,53 @@ +//// [stringLiteralTypeAssertion01.ts] + +type S = "a" | "b"; +type T = S[] | S; + +var s: S; +var t: T; +var str: string; + +//////////////// + +s = t; +s = t as S; + +s = str; +s = str as S; + +//////////////// + +t = s; +t = s as T; + +t = str; +t = str as T; + +//////////////// + +str = s; +str = s as string; + +str = t; +str = t as string; + + +//// [stringLiteralTypeAssertion01.js] +var s; +var t; +var str; +//////////////// +s = t; +s = t; +s = str; +s = str; +//////////////// +t = s; +t = s; +t = str; +t = str; +//////////////// +str = s; +str = s; +str = t; +str = t; diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.symbols b/tests/baselines/reference/stringLiteralTypeAssertion01.symbols new file mode 100644 index 0000000000000..1491a32b6f8bf --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypeAssertion01.symbols @@ -0,0 +1,83 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts === + +type S = "a" | "b"; +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +type T = S[] | S; +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +var s: S; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +var t: T; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) + +var str: string; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) + +//////////////// + +s = t; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) + +s = t as S; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +s = str; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) + +s = str as S; +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>S : Symbol(S, Decl(stringLiteralTypeAssertion01.ts, 0, 0)) + +//////////////// + +t = s; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) + +t = s as T; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) + +t = str; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) + +t = str as T; +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>T : Symbol(T, Decl(stringLiteralTypeAssertion01.ts, 1, 19)) + +//////////////// + +str = s; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) + +str = s as string; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>s : Symbol(s, Decl(stringLiteralTypeAssertion01.ts, 4, 3)) + +str = t; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) + +str = t as string; +>str : Symbol(str, Decl(stringLiteralTypeAssertion01.ts, 6, 3)) +>t : Symbol(t, Decl(stringLiteralTypeAssertion01.ts, 5, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypeAssertion01.types b/tests/baselines/reference/stringLiteralTypeAssertion01.types new file mode 100644 index 0000000000000..f70313f834700 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypeAssertion01.types @@ -0,0 +1,107 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts === + +type S = "a" | "b"; +>S : "a" | "b" + +type T = S[] | S; +>T : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" +>S : "a" | "b" + +var s: S; +>s : "a" | "b" +>S : "a" | "b" + +var t: T; +>t : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + +var str: string; +>str : string + +//////////////// + +s = t; +>s = t : "a" | "b" +>s : "a" | "b" +>t : "a" | "b" +>S : "a" | "b" +>t : ("a" | "b")[] | "a" | "b" + +s = t as S; +>s = t as S : "a" | "b" +>s : "a" | "b" +>t as S : "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>S : "a" | "b" + +s = str; +>s = str : "a" | "b" +>s : "a" | "b" +>str : "a" | "b" +>S : "a" | "b" +>str : string + +s = str as S; +>s = str as S : "a" | "b" +>s : "a" | "b" +>str as S : "a" | "b" +>str : string +>S : "a" | "b" + +//////////////// + +t = s; +>t = s : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>s : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" +>s : "a" | "b" + +t = s as T; +>t = s as T : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>s as T : ("a" | "b")[] | "a" | "b" +>s : "a" | "b" +>T : ("a" | "b")[] | "a" | "b" + +t = str; +>t = str : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>str : ("a" | "b")[] | "a" | "b" +>T : ("a" | "b")[] | "a" | "b" +>str : string + +t = str as T; +>t = str as T : ("a" | "b")[] | "a" | "b" +>t : ("a" | "b")[] | "a" | "b" +>str as T : ("a" | "b")[] | "a" | "b" +>str : string +>T : ("a" | "b")[] | "a" | "b" + +//////////////// + +str = s; +>str = s : string +>str : string +>s : string +>s : "a" | "b" + +str = s as string; +>str = s as string : string +>str : string +>s as string : string +>s : "a" | "b" + +str = t; +>str = t : string +>str : string +>t : string +>t : ("a" | "b")[] | "a" | "b" + +str = t as string; +>str = t as string : string +>str : string +>t as string : string +>t : ("a" | "b")[] | "a" | "b" + diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.js b/tests/baselines/reference/stringLiteralTypesAndTuples01.js new file mode 100644 index 0000000000000..ae02d12429a39 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.js @@ -0,0 +1,42 @@ +//// [stringLiteralTypesAndTuples01.ts] + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; + +type RexOrRaptor = "t-rex" | "raptor" +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; + +rawr(dinosaur); + +function rawr(dino: RexOrRaptor) { + if (dino === "t-rex") { + return "ROAAAAR!"; + } + if (dino === "raptor") { + return "yip yip!"; + } + + throw "Unexpected " + dino; +} + +//// [stringLiteralTypesAndTuples01.js] +// Should all be strings. +var _a = ["Hello", "Brave", "New", "World"], hello = _a[0], brave = _a[1], newish = _a[2], world = _a[3]; +var _b = ['I\'m', 'a', 't-rex'], im = _b[0], a = _b[1], dinosaur = _b[2]; +rawr(dinosaur); +function rawr(dino) { + if (dino === "t-rex") { + return "ROAAAAR!"; + } + if (dino === "raptor") { + return "yip yip!"; + } + throw "Unexpected " + dino; +} + + +//// [stringLiteralTypesAndTuples01.d.ts] +declare let hello: string, brave: string, newish: string, world: string; +declare type RexOrRaptor = "t-rex" | "raptor"; +declare let im: "I'm", a: "a", dinosaur: "t-rex" | "raptor"; +declare function rawr(dino: RexOrRaptor): string; diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.symbols b/tests/baselines/reference/stringLiteralTypesAndTuples01.symbols new file mode 100644 index 0000000000000..b7ca53517ca15 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.symbols @@ -0,0 +1,41 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts === + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; +>hello : Symbol(hello, Decl(stringLiteralTypesAndTuples01.ts, 2, 5)) +>brave : Symbol(brave, Decl(stringLiteralTypesAndTuples01.ts, 2, 11)) +>newish : Symbol(newish, Decl(stringLiteralTypesAndTuples01.ts, 2, 18)) +>world : Symbol(world, Decl(stringLiteralTypesAndTuples01.ts, 2, 26)) + +type RexOrRaptor = "t-rex" | "raptor" +>RexOrRaptor : Symbol(RexOrRaptor, Decl(stringLiteralTypesAndTuples01.ts, 2, 71)) + +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; +>im : Symbol(im, Decl(stringLiteralTypesAndTuples01.ts, 5, 5)) +>a : Symbol(a, Decl(stringLiteralTypesAndTuples01.ts, 5, 8)) +>dinosaur : Symbol(dinosaur, Decl(stringLiteralTypesAndTuples01.ts, 5, 11)) +>RexOrRaptor : Symbol(RexOrRaptor, Decl(stringLiteralTypesAndTuples01.ts, 2, 71)) + +rawr(dinosaur); +>rawr : Symbol(rawr, Decl(stringLiteralTypesAndTuples01.ts, 7, 15)) +>dinosaur : Symbol(dinosaur, Decl(stringLiteralTypesAndTuples01.ts, 5, 11)) + +function rawr(dino: RexOrRaptor) { +>rawr : Symbol(rawr, Decl(stringLiteralTypesAndTuples01.ts, 7, 15)) +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) +>RexOrRaptor : Symbol(RexOrRaptor, Decl(stringLiteralTypesAndTuples01.ts, 2, 71)) + + if (dino === "t-rex") { +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) + + return "ROAAAAR!"; + } + if (dino === "raptor") { +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) + + return "yip yip!"; + } + + throw "Unexpected " + dino; +>dino : Symbol(dino, Decl(stringLiteralTypesAndTuples01.ts, 9, 14)) +} diff --git a/tests/baselines/reference/stringLiteralTypesAndTuples01.types b/tests/baselines/reference/stringLiteralTypesAndTuples01.types new file mode 100644 index 0000000000000..c874973e3e590 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAndTuples01.types @@ -0,0 +1,59 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts === + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; +>hello : string +>brave : string +>newish : string +>world : string +>["Hello", "Brave", "New", "World"] : [string, string, string, string] +>"Hello" : string +>"Brave" : string +>"New" : string +>"World" : string + +type RexOrRaptor = "t-rex" | "raptor" +>RexOrRaptor : "t-rex" | "raptor" + +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; +>im : "I'm" +>a : "a" +>dinosaur : "t-rex" | "raptor" +>RexOrRaptor : "t-rex" | "raptor" +>['I\'m', 'a', 't-rex'] : ["I'm", "a", "t-rex"] +>'I\'m' : "I'm" +>'a' : "a" +>'t-rex' : "t-rex" + +rawr(dinosaur); +>rawr(dinosaur) : string +>rawr : (dino: "t-rex" | "raptor") => string +>dinosaur : "t-rex" | "raptor" + +function rawr(dino: RexOrRaptor) { +>rawr : (dino: "t-rex" | "raptor") => string +>dino : "t-rex" | "raptor" +>RexOrRaptor : "t-rex" | "raptor" + + if (dino === "t-rex") { +>dino === "t-rex" : boolean +>dino : "t-rex" | "raptor" +>"t-rex" : string + + return "ROAAAAR!"; +>"ROAAAAR!" : string + } + if (dino === "raptor") { +>dino === "raptor" : boolean +>dino : "t-rex" | "raptor" +>"raptor" : string + + return "yip yip!"; +>"yip yip!" : string + } + + throw "Unexpected " + dino; +>"Unexpected " + dino : string +>"Unexpected " : string +>dino : "t-rex" | "raptor" +} diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt new file mode 100644 index 0000000000000..6e4f8943fb6af --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt @@ -0,0 +1,57 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(18,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(19,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(20,10): error TS2394: Overload signature is not compatible with function implementation. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21): error TS2304: Cannot find name 'is'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (4 errors) ==== + + type Kind = "A" | "B" + + interface Entity { + kind: Kind; + } + + interface A extends Entity { + kind: "A"; + a: number; + } + + interface B extends Entity { + kind: "B"; + b: string; + } + + function hasKind(entity: Entity, kind: "A"): entity is A; + ~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function hasKind(entity: Entity, kind: "B"): entity is B; + ~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function hasKind(entity: Entity, kind: Kind): entity is Entity; + ~~~~~~~ +!!! error TS2394: Overload signature is not compatible with function implementation. + function hasKind(entity: Entity, kind: Kind): boolean { + return kind === is; + ~~ +!!! error TS2304: Cannot find name 'is'. + } + + let x: A = { + kind: "A", + a: 100, + } + + if (hasKind(x, "A")) { + let a = x; + } + else { + let b = x; + } + + if (!hasKind(x, "B")) { + let c = x; + } + else { + let d = x; + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.js b/tests/baselines/reference/stringLiteralTypesAsTags01.js new file mode 100644 index 0000000000000..3fe33ace4870a --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.js @@ -0,0 +1,83 @@ +//// [stringLiteralTypesAsTags01.ts] + +type Kind = "A" | "B" + +interface Entity { + kind: Kind; +} + +interface A extends Entity { + kind: "A"; + a: number; +} + +interface B extends Entity { + kind: "B"; + b: string; +} + +function hasKind(entity: Entity, kind: "A"): entity is A; +function hasKind(entity: Entity, kind: "B"): entity is B; +function hasKind(entity: Entity, kind: Kind): entity is Entity; +function hasKind(entity: Entity, kind: Kind): boolean { + return kind === is; +} + +let x: A = { + kind: "A", + a: 100, +} + +if (hasKind(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!hasKind(x, "B")) { + let c = x; +} +else { + let d = x; +} + +//// [stringLiteralTypesAsTags01.js] +function hasKind(entity, kind) { + return kind === is; +} +var x = { + kind: "A", + a: 100 +}; +if (hasKind(x, "A")) { + var a = x; +} +else { + var b = x; +} +if (!hasKind(x, "B")) { + var c = x; +} +else { + var d = x; +} + + +//// [stringLiteralTypesAsTags01.d.ts] +declare type Kind = "A" | "B"; +interface Entity { + kind: Kind; +} +interface A extends Entity { + kind: "A"; + a: number; +} +interface B extends Entity { + kind: "B"; + b: string; +} +declare function hasKind(entity: Entity, kind: "A"): entity is A; +declare function hasKind(entity: Entity, kind: "B"): entity is B; +declare function hasKind(entity: Entity, kind: Kind): entity is Entity; +declare let x: A; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js new file mode 100644 index 0000000000000..d970310d68e32 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.js @@ -0,0 +1,44 @@ +//// [stringLiteralTypesInUnionTypes01.ts] + +type T = "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; + +//// [stringLiteralTypesInUnionTypes01.js] +var x = "foo"; +var y = "bar"; +if (x === "foo") { + var a = x; +} +else if (x !== "bar") { + var b = x || y; +} +else { + var c = x; + var d = y; + var e = c || d; +} +x = y; +y = x; + + +//// [stringLiteralTypesInUnionTypes01.d.ts] +declare type T = "foo" | "bar" | "baz"; +declare var x: "foo" | "bar" | "baz"; +declare var y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.symbols new file mode 100644 index 0000000000000..c608929383ed3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.symbols @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts === + +type T = "foo" | "bar" | "baz"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes01.ts, 0, 0)) + +var x: "foo" | "bar" | "baz" = "foo"; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + +var y: T = "bar"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes01.ts, 0, 0)) + +if (x === "foo") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes01.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +} +else if (x !== "bar") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + + let b = x || y; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes01.ts, 10, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +} +else { + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes01.ts, 13, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + + let d = y; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes01.ts, 14, 7)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) + + let e: (typeof x) | (typeof y) = c || d; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes01.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes01.ts, 13, 7)) +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes01.ts, 14, 7)) +} + +x = y; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) + +y = x; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes01.ts, 4, 3)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes01.ts, 3, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types new file mode 100644 index 0000000000000..e5ff509822b99 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes01.types @@ -0,0 +1,62 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts === + +type T = "foo" | "bar" | "baz"; +>T : "foo" | "bar" | "baz" + +var x: "foo" | "bar" | "baz" = "foo"; +>x : "foo" | "bar" | "baz" +>"foo" : "foo" + +var y: T = "bar"; +>y : "foo" | "bar" | "baz" +>T : "foo" | "bar" | "baz" +>"bar" : "bar" + +if (x === "foo") { +>x === "foo" : boolean +>x : "foo" | "bar" | "baz" +>"foo" : string + + let a = x; +>a : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +} +else if (x !== "bar") { +>x !== "bar" : boolean +>x : "foo" | "bar" | "baz" +>"bar" : string + + let b = x || y; +>b : "foo" | "bar" | "baz" +>x || y : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" +} +else { + let c = x; +>c : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" + + let d = y; +>d : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" + + let e: (typeof x) | (typeof y) = c || d; +>e : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" +>c || d : "foo" | "bar" | "baz" +>c : "foo" | "bar" | "baz" +>d : "foo" | "bar" | "baz" +} + +x = y; +>x = y : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" + +y = x; +>y = x : "foo" | "bar" | "baz" +>y : "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js new file mode 100644 index 0000000000000..19b9c837c9da8 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.js @@ -0,0 +1,44 @@ +//// [stringLiteralTypesInUnionTypes02.ts] + +type T = string | "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" | string = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; + +//// [stringLiteralTypesInUnionTypes02.js] +var x = "foo"; +var y = "bar"; +if (x === "foo") { + var a = x; +} +else if (x !== "bar") { + var b = x || y; +} +else { + var c = x; + var d = y; + var e = c || d; +} +x = y; +y = x; + + +//// [stringLiteralTypesInUnionTypes02.d.ts] +declare type T = string | "foo" | "bar" | "baz"; +declare var x: "foo" | "bar" | "baz" | string; +declare var y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.symbols new file mode 100644 index 0000000000000..c9b31dc710a02 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.symbols @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts === + +type T = string | "foo" | "bar" | "baz"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes02.ts, 0, 0)) + +var x: "foo" | "bar" | "baz" | string = "foo"; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + +var y: T = "bar"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes02.ts, 0, 0)) + +if (x === "foo") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes02.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +} +else if (x !== "bar") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + + let b = x || y; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes02.ts, 10, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +} +else { + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes02.ts, 13, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + + let d = y; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes02.ts, 14, 7)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) + + let e: (typeof x) | (typeof y) = c || d; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes02.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes02.ts, 13, 7)) +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes02.ts, 14, 7)) +} + +x = y; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) + +y = x; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes02.ts, 4, 3)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes02.ts, 3, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types new file mode 100644 index 0000000000000..b468c620376ba --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types @@ -0,0 +1,62 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts === + +type T = string | "foo" | "bar" | "baz"; +>T : string | "foo" | "bar" | "baz" + +var x: "foo" | "bar" | "baz" | string = "foo"; +>x : "foo" | "bar" | "baz" | string +>"foo" : "foo" + +var y: T = "bar"; +>y : string | "foo" | "bar" | "baz" +>T : string | "foo" | "bar" | "baz" +>"bar" : "bar" + +if (x === "foo") { +>x === "foo" : boolean +>x : "foo" | "bar" | "baz" | string +>"foo" : string + + let a = x; +>a : "foo" | "bar" | "baz" | string +>x : "foo" | "bar" | "baz" | string +} +else if (x !== "bar") { +>x !== "bar" : boolean +>x : "foo" | "bar" | "baz" | string +>"bar" : string + + let b = x || y; +>b : string +>x || y : string +>x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" +} +else { + let c = x; +>c : "foo" | "bar" | "baz" | string +>x : "foo" | "bar" | "baz" | string + + let d = y; +>d : string | "foo" | "bar" | "baz" +>y : string | "foo" | "bar" | "baz" + + let e: (typeof x) | (typeof y) = c || d; +>e : "foo" | "bar" | "baz" | string +>x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" +>c || d : string +>c : "foo" | "bar" | "baz" | string +>d : string | "foo" | "bar" | "baz" +} + +x = y; +>x = y : string | "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" + +y = x; +>y = x : "foo" | "bar" | "baz" | string +>y : string | "foo" | "bar" | "baz" +>x : "foo" | "bar" | "baz" | string + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js new file mode 100644 index 0000000000000..f6728e0b2fb2f --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.js @@ -0,0 +1,44 @@ +//// [stringLiteralTypesInUnionTypes03.ts] + +type T = number | "foo" | "bar"; + +var x: "foo" | "bar" | number; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; + +//// [stringLiteralTypesInUnionTypes03.js] +var x; +var y = "bar"; +if (x === "foo") { + var a = x; +} +else if (x !== "bar") { + var b = x || y; +} +else { + var c = x; + var d = y; + var e = c || d; +} +x = y; +y = x; + + +//// [stringLiteralTypesInUnionTypes03.d.ts] +declare type T = number | "foo" | "bar"; +declare var x: "foo" | "bar" | number; +declare var y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.symbols new file mode 100644 index 0000000000000..df5498d9a5943 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.symbols @@ -0,0 +1,52 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts === + +type T = number | "foo" | "bar"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes03.ts, 0, 0)) + +var x: "foo" | "bar" | number; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + +var y: T = "bar"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes03.ts, 0, 0)) + +if (x === "foo") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes03.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +} +else if (x !== "bar") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + + let b = x || y; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes03.ts, 10, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +} +else { + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes03.ts, 13, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + + let d = y; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes03.ts, 14, 7)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) + + let e: (typeof x) | (typeof y) = c || d; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes03.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes03.ts, 13, 7)) +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes03.ts, 14, 7)) +} + +x = y; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) + +y = x; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes03.ts, 4, 3)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes03.ts, 3, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes03.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.types new file mode 100644 index 0000000000000..5fca6e69be996 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes03.types @@ -0,0 +1,61 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts === + +type T = number | "foo" | "bar"; +>T : number | "foo" | "bar" + +var x: "foo" | "bar" | number; +>x : "foo" | "bar" | number + +var y: T = "bar"; +>y : number | "foo" | "bar" +>T : number | "foo" | "bar" +>"bar" : "bar" + +if (x === "foo") { +>x === "foo" : boolean +>x : "foo" | "bar" | number +>"foo" : string + + let a = x; +>a : "foo" | "bar" | number +>x : "foo" | "bar" | number +} +else if (x !== "bar") { +>x !== "bar" : boolean +>x : "foo" | "bar" | number +>"bar" : string + + let b = x || y; +>b : "foo" | "bar" | number +>x || y : "foo" | "bar" | number +>x : "foo" | "bar" | number +>y : number | "foo" | "bar" +} +else { + let c = x; +>c : "foo" | "bar" | number +>x : "foo" | "bar" | number + + let d = y; +>d : number | "foo" | "bar" +>y : number | "foo" | "bar" + + let e: (typeof x) | (typeof y) = c || d; +>e : "foo" | "bar" | number +>x : "foo" | "bar" | number +>y : number | "foo" | "bar" +>c || d : "foo" | "bar" | number +>c : "foo" | "bar" | number +>d : number | "foo" | "bar" +} + +x = y; +>x = y : number | "foo" | "bar" +>x : "foo" | "bar" | number +>y : number | "foo" | "bar" + +y = x; +>y = x : "foo" | "bar" | number +>y : number | "foo" | "bar" +>x : "foo" | "bar" | number + diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js new file mode 100644 index 0000000000000..dc6cf51ad1e1c --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.js @@ -0,0 +1,72 @@ +//// [stringLiteralTypesInUnionTypes04.ts] + +type T = "" | "foo"; + +let x: T = ""; +let y: T = "foo"; + +if (x === "") { + let a = x; +} + +if (x !== "") { + let b = x; +} + +if (x == "") { + let c = x; +} + +if (x != "") { + let d = x; +} + +if (x) { + let e = x; +} + +if (!x) { + let f = x; +} + +if (!!x) { + let g = x; +} + +if (!!!x) { + let h = x; +} + +//// [stringLiteralTypesInUnionTypes04.js] +var x = ""; +var y = "foo"; +if (x === "") { + var a = x; +} +if (x !== "") { + var b = x; +} +if (x == "") { + var c = x; +} +if (x != "") { + var d = x; +} +if (x) { + var e = x; +} +if (!x) { + var f = x; +} +if (!!x) { + var g = x; +} +if (!!!x) { + var h = x; +} + + +//// [stringLiteralTypesInUnionTypes04.d.ts] +declare type T = "" | "foo"; +declare let x: T; +declare let y: T; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.symbols b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.symbols new file mode 100644 index 0000000000000..ced93fcefc98f --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.symbols @@ -0,0 +1,76 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts === + +type T = "" | "foo"; +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes04.ts, 0, 0)) + +let x: T = ""; +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes04.ts, 0, 0)) + +let y: T = "foo"; +>y : Symbol(y, Decl(stringLiteralTypesInUnionTypes04.ts, 4, 3)) +>T : Symbol(T, Decl(stringLiteralTypesInUnionTypes04.ts, 0, 0)) + +if (x === "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let a = x; +>a : Symbol(a, Decl(stringLiteralTypesInUnionTypes04.ts, 7, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x !== "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let b = x; +>b : Symbol(b, Decl(stringLiteralTypesInUnionTypes04.ts, 11, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x == "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let c = x; +>c : Symbol(c, Decl(stringLiteralTypesInUnionTypes04.ts, 15, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x != "") { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let d = x; +>d : Symbol(d, Decl(stringLiteralTypesInUnionTypes04.ts, 19, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let e = x; +>e : Symbol(e, Decl(stringLiteralTypesInUnionTypes04.ts, 23, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (!x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let f = x; +>f : Symbol(f, Decl(stringLiteralTypesInUnionTypes04.ts, 27, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (!!x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let g = x; +>g : Symbol(g, Decl(stringLiteralTypesInUnionTypes04.ts, 31, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} + +if (!!!x) { +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) + + let h = x; +>h : Symbol(h, Decl(stringLiteralTypesInUnionTypes04.ts, 35, 7)) +>x : Symbol(x, Decl(stringLiteralTypesInUnionTypes04.ts, 3, 3)) +} diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types new file mode 100644 index 0000000000000..ad92dc360d0a9 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes04.types @@ -0,0 +1,92 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts === + +type T = "" | "foo"; +>T : "" | "foo" + +let x: T = ""; +>x : "" | "foo" +>T : "" | "foo" +>"" : "" + +let y: T = "foo"; +>y : "" | "foo" +>T : "" | "foo" +>"foo" : "foo" + +if (x === "") { +>x === "" : boolean +>x : "" | "foo" +>"" : string + + let a = x; +>a : "" | "foo" +>x : "" | "foo" +} + +if (x !== "") { +>x !== "" : boolean +>x : "" | "foo" +>"" : string + + let b = x; +>b : "" | "foo" +>x : "" | "foo" +} + +if (x == "") { +>x == "" : boolean +>x : "" | "foo" +>"" : string + + let c = x; +>c : "" | "foo" +>x : "" | "foo" +} + +if (x != "") { +>x != "" : boolean +>x : "" | "foo" +>"" : string + + let d = x; +>d : "" | "foo" +>x : "" | "foo" +} + +if (x) { +>x : "" | "foo" + + let e = x; +>e : "" | "foo" +>x : "" | "foo" +} + +if (!x) { +>!x : boolean +>x : "" | "foo" + + let f = x; +>f : "" | "foo" +>x : "" | "foo" +} + +if (!!x) { +>!!x : boolean +>!x : boolean +>x : "" | "foo" + + let g = x; +>g : "" | "foo" +>x : "" | "foo" +} + +if (!!!x) { +>!!!x : boolean +>!!x : boolean +>!x : boolean +>x : "" | "foo" + + let h = x; +>h : "" | "foo" +>x : "" | "foo" +} diff --git a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt new file mode 100644 index 0000000000000..1a639a1c22284 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts(5,7): error TS1155: 'const' declarations must be initialized + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts (1 errors) ==== + + let a: ""; + var b: "foo"; + let c: "bar"; + const d: "baz"; + ~ +!!! error TS1155: 'const' declarations must be initialized + + a = ""; + b = "foo"; + c = "bar"; + + let e: "" = ""; + var f: "foo" = "foo"; + let g: "bar" = "bar"; + const h: "baz" = "baz"; + + e = ""; + f = "foo"; + g = "bar"; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js new file mode 100644 index 0000000000000..15a4ca5d580db --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesInVariableDeclarations01.js @@ -0,0 +1,46 @@ +//// [stringLiteralTypesInVariableDeclarations01.ts] + +let a: ""; +var b: "foo"; +let c: "bar"; +const d: "baz"; + +a = ""; +b = "foo"; +c = "bar"; + +let e: "" = ""; +var f: "foo" = "foo"; +let g: "bar" = "bar"; +const h: "baz" = "baz"; + +e = ""; +f = "foo"; +g = "bar"; + +//// [stringLiteralTypesInVariableDeclarations01.js] +var a; +var b; +var c; +var d; +a = ""; +b = "foo"; +c = "bar"; +var e = ""; +var f = "foo"; +var g = "bar"; +var h = "baz"; +e = ""; +f = "foo"; +g = "bar"; + + +//// [stringLiteralTypesInVariableDeclarations01.d.ts] +declare let a: ""; +declare var b: "foo"; +declare let c: "bar"; +declare const d: "baz"; +declare let e: ""; +declare var f: "foo"; +declare let g: "bar"; +declare const h: "baz"; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt new file mode 100644 index 0000000000000..1592427208b3d --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.errors.txt @@ -0,0 +1,59 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts(11,10): error TS2354: No best common type exists among return expressions. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts (1 errors) ==== + + type PrimitiveName = 'string' | 'number' | 'boolean'; + + function getFalsyPrimitive(x: "string"): string; + function getFalsyPrimitive(x: "number"): number; + function getFalsyPrimitive(x: "boolean"): boolean; + function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; + function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; + function getFalsyPrimitive(x: "number" | "string"): number | string; + function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; + function getFalsyPrimitive(x: PrimitiveName) { + ~~~~~~~~~~~~~~~~~ +!!! error TS2354: No best common type exists among return expressions. + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; + } + + namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); + } + + const string: "string" = "string" + const number: "number" = "number" + const boolean: "boolean" = "boolean" + + const stringOrNumber = string || number; + const stringOrBoolean = string || boolean; + const booleanOrNumber = number || boolean; + const stringOrBooleanOrNumber = stringOrBoolean || number; + + namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); + } + + + \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesOverloads01.js b/tests/baselines/reference/stringLiteralTypesOverloads01.js new file mode 100644 index 0000000000000..73dabd26fab18 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads01.js @@ -0,0 +1,114 @@ +//// [stringLiteralTypesOverloads01.ts] + +type PrimitiveName = 'string' | 'number' | 'boolean'; + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: PrimitiveName) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + + + +//// [stringLiteralTypesOverloads01.js] +function getFalsyPrimitive(x) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + // Should be unreachable. + throw "Invalid value"; +} +var Consts1; +(function (Consts1) { + var EMPTY_STRING = getFalsyPrimitive("string"); + var ZERO = getFalsyPrimitive('number'); + var FALSE = getFalsyPrimitive("boolean"); +})(Consts1 || (Consts1 = {})); +var string = "string"; +var number = "number"; +var boolean = "boolean"; +var stringOrNumber = string || number; +var stringOrBoolean = string || boolean; +var booleanOrNumber = number || boolean; +var stringOrBooleanOrNumber = stringOrBoolean || number; +var Consts2; +(function (Consts2) { + var EMPTY_STRING = getFalsyPrimitive(string); + var ZERO = getFalsyPrimitive(number); + var FALSE = getFalsyPrimitive(boolean); + var a = getFalsyPrimitive(stringOrNumber); + var b = getFalsyPrimitive(stringOrBoolean); + var c = getFalsyPrimitive(booleanOrNumber); + var d = getFalsyPrimitive(stringOrBooleanOrNumber); +})(Consts2 || (Consts2 = {})); + + +//// [stringLiteralTypesOverloads01.d.ts] +declare type PrimitiveName = 'string' | 'number' | 'boolean'; +declare function getFalsyPrimitive(x: "string"): string; +declare function getFalsyPrimitive(x: "number"): number; +declare function getFalsyPrimitive(x: "boolean"): boolean; +declare function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +declare function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +declare function getFalsyPrimitive(x: "number" | "string"): number | string; +declare function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +declare namespace Consts1 { +} +declare const string: "string"; +declare const number: "number"; +declare const boolean: "boolean"; +declare const stringOrNumber: "string" | "number"; +declare const stringOrBoolean: "string" | "boolean"; +declare const booleanOrNumber: "number" | "boolean"; +declare const stringOrBooleanOrNumber: "string" | "boolean" | "number"; +declare namespace Consts2 { +} diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt new file mode 100644 index 0000000000000..995cf687b702c --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.errors.txt @@ -0,0 +1,57 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts(9,10): error TS2354: No best common type exists among return expressions. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts (1 errors) ==== + + function getFalsyPrimitive(x: "string"): string; + function getFalsyPrimitive(x: "number"): number; + function getFalsyPrimitive(x: "boolean"): boolean; + function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; + function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; + function getFalsyPrimitive(x: "number" | "string"): number | string; + function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; + function getFalsyPrimitive(x: string) { + ~~~~~~~~~~~~~~~~~ +!!! error TS2354: No best common type exists among return expressions. + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; + } + + namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); + } + + const string: "string" = "string" + const number: "number" = "number" + const boolean: "boolean" = "boolean" + + const stringOrNumber = string || number; + const stringOrBoolean = string || boolean; + const booleanOrNumber = number || boolean; + const stringOrBooleanOrNumber = stringOrBoolean || number; + + namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); + } + + + \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesOverloads02.js b/tests/baselines/reference/stringLiteralTypesOverloads02.js new file mode 100644 index 0000000000000..3c53f9380a88b --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads02.js @@ -0,0 +1,111 @@ +//// [stringLiteralTypesOverloads02.ts] + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: string) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + + + +//// [stringLiteralTypesOverloads02.js] +function getFalsyPrimitive(x) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + // Should be unreachable. + throw "Invalid value"; +} +var Consts1; +(function (Consts1) { + var EMPTY_STRING = getFalsyPrimitive("string"); + var ZERO = getFalsyPrimitive('number'); + var FALSE = getFalsyPrimitive("boolean"); +})(Consts1 || (Consts1 = {})); +var string = "string"; +var number = "number"; +var boolean = "boolean"; +var stringOrNumber = string || number; +var stringOrBoolean = string || boolean; +var booleanOrNumber = number || boolean; +var stringOrBooleanOrNumber = stringOrBoolean || number; +var Consts2; +(function (Consts2) { + var EMPTY_STRING = getFalsyPrimitive(string); + var ZERO = getFalsyPrimitive(number); + var FALSE = getFalsyPrimitive(boolean); + var a = getFalsyPrimitive(stringOrNumber); + var b = getFalsyPrimitive(stringOrBoolean); + var c = getFalsyPrimitive(booleanOrNumber); + var d = getFalsyPrimitive(stringOrBooleanOrNumber); +})(Consts2 || (Consts2 = {})); + + +//// [stringLiteralTypesOverloads02.d.ts] +declare function getFalsyPrimitive(x: "string"): string; +declare function getFalsyPrimitive(x: "number"): number; +declare function getFalsyPrimitive(x: "boolean"): boolean; +declare function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +declare function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +declare function getFalsyPrimitive(x: "number" | "string"): number | string; +declare function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +declare namespace Consts1 { +} +declare const string: "string"; +declare const number: "number"; +declare const boolean: "boolean"; +declare const stringOrNumber: "string" | "number"; +declare const stringOrBoolean: "string" | "boolean"; +declare const booleanOrNumber: "number" | "boolean"; +declare const stringOrBooleanOrNumber: "string" | "boolean" | "number"; +declare namespace Consts2 { +} diff --git a/tests/baselines/reference/stringLiteralTypesOverloads03.js b/tests/baselines/reference/stringLiteralTypesOverloads03.js new file mode 100644 index 0000000000000..c1e7916088095 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads03.js @@ -0,0 +1,104 @@ +//// [stringLiteralTypesOverloads03.ts] + +interface Base { + x: string; + y: number; +} + +interface HelloOrWorld extends Base { + p1: boolean; +} + +interface JustHello extends Base { + p2: boolean; +} + +interface JustWorld extends Base { + p3: boolean; +} + +let hello: "hello"; +let world: "world"; +let helloOrWorld: "hello" | "world"; + +function f(p: "hello"): JustHello; +function f(p: "hello" | "world"): HelloOrWorld; +function f(p: "world"): JustWorld; +function f(p: string): Base; +function f(...args: any[]): any { + return undefined; +} + +let fResult1 = f(hello); +let fResult2 = f(world); +let fResult3 = f(helloOrWorld); + +function g(p: string): Base; +function g(p: "hello"): JustHello; +function g(p: "hello" | "world"): HelloOrWorld; +function g(p: "world"): JustWorld; +function g(...args: any[]): any { + return undefined; +} + +let gResult1 = g(hello); +let gResult2 = g(world); +let gResult3 = g(helloOrWorld); + +//// [stringLiteralTypesOverloads03.js] +var hello; +var world; +var helloOrWorld; +function f() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i - 0] = arguments[_i]; + } + return undefined; +} +var fResult1 = f(hello); +var fResult2 = f(world); +var fResult3 = f(helloOrWorld); +function g() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i - 0] = arguments[_i]; + } + return undefined; +} +var gResult1 = g(hello); +var gResult2 = g(world); +var gResult3 = g(helloOrWorld); + + +//// [stringLiteralTypesOverloads03.d.ts] +interface Base { + x: string; + y: number; +} +interface HelloOrWorld extends Base { + p1: boolean; +} +interface JustHello extends Base { + p2: boolean; +} +interface JustWorld extends Base { + p3: boolean; +} +declare let hello: "hello"; +declare let world: "world"; +declare let helloOrWorld: "hello" | "world"; +declare function f(p: "hello"): JustHello; +declare function f(p: "hello" | "world"): HelloOrWorld; +declare function f(p: "world"): JustWorld; +declare function f(p: string): Base; +declare let fResult1: JustHello; +declare let fResult2: JustWorld; +declare let fResult3: HelloOrWorld; +declare function g(p: string): Base; +declare function g(p: "hello"): JustHello; +declare function g(p: "hello" | "world"): HelloOrWorld; +declare function g(p: "world"): JustWorld; +declare let gResult1: JustHello; +declare let gResult2: JustWorld; +declare let gResult3: Base; diff --git a/tests/baselines/reference/stringLiteralTypesOverloads03.symbols b/tests/baselines/reference/stringLiteralTypesOverloads03.symbols new file mode 100644 index 0000000000000..a0a48de790f93 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads03.symbols @@ -0,0 +1,131 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts === + +interface Base { +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + x: string; +>x : Symbol(x, Decl(stringLiteralTypesOverloads03.ts, 1, 16)) + + y: number; +>y : Symbol(y, Decl(stringLiteralTypesOverloads03.ts, 2, 14)) +} + +interface HelloOrWorld extends Base { +>HelloOrWorld : Symbol(HelloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 4, 1)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + p1: boolean; +>p1 : Symbol(p1, Decl(stringLiteralTypesOverloads03.ts, 6, 37)) +} + +interface JustHello extends Base { +>JustHello : Symbol(JustHello, Decl(stringLiteralTypesOverloads03.ts, 8, 1)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + p2: boolean; +>p2 : Symbol(p2, Decl(stringLiteralTypesOverloads03.ts, 10, 34)) +} + +interface JustWorld extends Base { +>JustWorld : Symbol(JustWorld, Decl(stringLiteralTypesOverloads03.ts, 12, 1)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + + p3: boolean; +>p3 : Symbol(p3, Decl(stringLiteralTypesOverloads03.ts, 14, 34)) +} + +let hello: "hello"; +>hello : Symbol(hello, Decl(stringLiteralTypesOverloads03.ts, 18, 3)) + +let world: "world"; +>world : Symbol(world, Decl(stringLiteralTypesOverloads03.ts, 19, 3)) + +let helloOrWorld: "hello" | "world"; +>helloOrWorld : Symbol(helloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 20, 3)) + +function f(p: "hello"): JustHello; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 22, 11)) +>JustHello : Symbol(JustHello, Decl(stringLiteralTypesOverloads03.ts, 8, 1)) + +function f(p: "hello" | "world"): HelloOrWorld; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 23, 11)) +>HelloOrWorld : Symbol(HelloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 4, 1)) + +function f(p: "world"): JustWorld; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 24, 11)) +>JustWorld : Symbol(JustWorld, Decl(stringLiteralTypesOverloads03.ts, 12, 1)) + +function f(p: string): Base; +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 25, 11)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + +function f(...args: any[]): any { +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>args : Symbol(args, Decl(stringLiteralTypesOverloads03.ts, 26, 11)) + + return undefined; +>undefined : Symbol(undefined) +} + +let fResult1 = f(hello); +>fResult1 : Symbol(fResult1, Decl(stringLiteralTypesOverloads03.ts, 30, 3)) +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>hello : Symbol(hello, Decl(stringLiteralTypesOverloads03.ts, 18, 3)) + +let fResult2 = f(world); +>fResult2 : Symbol(fResult2, Decl(stringLiteralTypesOverloads03.ts, 31, 3)) +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>world : Symbol(world, Decl(stringLiteralTypesOverloads03.ts, 19, 3)) + +let fResult3 = f(helloOrWorld); +>fResult3 : Symbol(fResult3, Decl(stringLiteralTypesOverloads03.ts, 32, 3)) +>f : Symbol(f, Decl(stringLiteralTypesOverloads03.ts, 20, 36), Decl(stringLiteralTypesOverloads03.ts, 22, 34), Decl(stringLiteralTypesOverloads03.ts, 23, 47), Decl(stringLiteralTypesOverloads03.ts, 24, 34), Decl(stringLiteralTypesOverloads03.ts, 25, 28)) +>helloOrWorld : Symbol(helloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 20, 3)) + +function g(p: string): Base; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 34, 11)) +>Base : Symbol(Base, Decl(stringLiteralTypesOverloads03.ts, 0, 0)) + +function g(p: "hello"): JustHello; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 35, 11)) +>JustHello : Symbol(JustHello, Decl(stringLiteralTypesOverloads03.ts, 8, 1)) + +function g(p: "hello" | "world"): HelloOrWorld; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 36, 11)) +>HelloOrWorld : Symbol(HelloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 4, 1)) + +function g(p: "world"): JustWorld; +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>p : Symbol(p, Decl(stringLiteralTypesOverloads03.ts, 37, 11)) +>JustWorld : Symbol(JustWorld, Decl(stringLiteralTypesOverloads03.ts, 12, 1)) + +function g(...args: any[]): any { +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>args : Symbol(args, Decl(stringLiteralTypesOverloads03.ts, 38, 11)) + + return undefined; +>undefined : Symbol(undefined) +} + +let gResult1 = g(hello); +>gResult1 : Symbol(gResult1, Decl(stringLiteralTypesOverloads03.ts, 42, 3)) +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>hello : Symbol(hello, Decl(stringLiteralTypesOverloads03.ts, 18, 3)) + +let gResult2 = g(world); +>gResult2 : Symbol(gResult2, Decl(stringLiteralTypesOverloads03.ts, 43, 3)) +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>world : Symbol(world, Decl(stringLiteralTypesOverloads03.ts, 19, 3)) + +let gResult3 = g(helloOrWorld); +>gResult3 : Symbol(gResult3, Decl(stringLiteralTypesOverloads03.ts, 44, 3)) +>g : Symbol(g, Decl(stringLiteralTypesOverloads03.ts, 32, 31), Decl(stringLiteralTypesOverloads03.ts, 34, 28), Decl(stringLiteralTypesOverloads03.ts, 35, 34), Decl(stringLiteralTypesOverloads03.ts, 36, 47), Decl(stringLiteralTypesOverloads03.ts, 37, 34)) +>helloOrWorld : Symbol(helloOrWorld, Decl(stringLiteralTypesOverloads03.ts, 20, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesOverloads03.types b/tests/baselines/reference/stringLiteralTypesOverloads03.types new file mode 100644 index 0000000000000..643979ee987ca --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesOverloads03.types @@ -0,0 +1,137 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts === + +interface Base { +>Base : Base + + x: string; +>x : string + + y: number; +>y : number +} + +interface HelloOrWorld extends Base { +>HelloOrWorld : HelloOrWorld +>Base : Base + + p1: boolean; +>p1 : boolean +} + +interface JustHello extends Base { +>JustHello : JustHello +>Base : Base + + p2: boolean; +>p2 : boolean +} + +interface JustWorld extends Base { +>JustWorld : JustWorld +>Base : Base + + p3: boolean; +>p3 : boolean +} + +let hello: "hello"; +>hello : "hello" + +let world: "world"; +>world : "world" + +let helloOrWorld: "hello" | "world"; +>helloOrWorld : "hello" | "world" + +function f(p: "hello"): JustHello; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : "hello" +>JustHello : JustHello + +function f(p: "hello" | "world"): HelloOrWorld; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : "hello" | "world" +>HelloOrWorld : HelloOrWorld + +function f(p: "world"): JustWorld; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : "world" +>JustWorld : JustWorld + +function f(p: string): Base; +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>p : string +>Base : Base + +function f(...args: any[]): any { +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>args : any[] + + return undefined; +>undefined : undefined +} + +let fResult1 = f(hello); +>fResult1 : JustHello +>f(hello) : JustHello +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>hello : "hello" + +let fResult2 = f(world); +>fResult2 : JustWorld +>f(world) : JustWorld +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>world : "world" + +let fResult3 = f(helloOrWorld); +>fResult3 : HelloOrWorld +>f(helloOrWorld) : HelloOrWorld +>f : { (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; (p: string): Base; } +>helloOrWorld : "hello" | "world" + +function g(p: string): Base; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : string +>Base : Base + +function g(p: "hello"): JustHello; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : "hello" +>JustHello : JustHello + +function g(p: "hello" | "world"): HelloOrWorld; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : "hello" | "world" +>HelloOrWorld : HelloOrWorld + +function g(p: "world"): JustWorld; +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>p : "world" +>JustWorld : JustWorld + +function g(...args: any[]): any { +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>args : any[] + + return undefined; +>undefined : undefined +} + +let gResult1 = g(hello); +>gResult1 : JustHello +>g(hello) : JustHello +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>hello : "hello" + +let gResult2 = g(world); +>gResult2 : JustWorld +>g(world) : JustWorld +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>world : "world" + +let gResult3 = g(helloOrWorld); +>gResult3 : Base +>g(helloOrWorld) : Base +>g : { (p: string): Base; (p: "hello"): JustHello; (p: "hello" | "world"): HelloOrWorld; (p: "world"): JustWorld; } +>helloOrWorld : "hello" | "world" + diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt new file mode 100644 index 0000000000000..b888c3ca77e40 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.errors.txt @@ -0,0 +1,33 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts(5,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts (2 errors) ==== + + type Kind = "A" | "B" + + function kindIs(kind: Kind, is: "A"): kind is "A"; + ~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function kindIs(kind: Kind, is: "B"): kind is "B"; + ~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function kindIs(kind: Kind, is: Kind): boolean { + return kind === is; + } + + var x: Kind = "A"; + + if (kindIs(x, "A")) { + let a = x; + } + else { + let b = x; + } + + if (!kindIs(x, "B")) { + let c = x; + } + else { + let d = x; + } \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesTypePredicates01.js b/tests/baselines/reference/stringLiteralTypesTypePredicates01.js new file mode 100644 index 0000000000000..fd4129d272d05 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesTypePredicates01.js @@ -0,0 +1,50 @@ +//// [stringLiteralTypesTypePredicates01.ts] + +type Kind = "A" | "B" + +function kindIs(kind: Kind, is: "A"): kind is "A"; +function kindIs(kind: Kind, is: "B"): kind is "B"; +function kindIs(kind: Kind, is: Kind): boolean { + return kind === is; +} + +var x: Kind = "A"; + +if (kindIs(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!kindIs(x, "B")) { + let c = x; +} +else { + let d = x; +} + +//// [stringLiteralTypesTypePredicates01.js] +function kindIs(kind, is) { + return kind === is; +} +var x = "A"; +if (kindIs(x, "A")) { + var a = x; +} +else { + var b = x; +} +if (!kindIs(x, "B")) { + var c = x; +} +else { + var d = x; +} + + +//// [stringLiteralTypesTypePredicates01.d.ts] +declare type Kind = "A" | "B"; +declare function kindIs(kind: Kind, is: "A"): kind is "A"; +declare function kindIs(kind: Kind, is: "B"): kind is "B"; +declare var x: Kind; diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.errors.txt b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.errors.txt new file mode 100644 index 0000000000000..3851a0b10efaa --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.errors.txt @@ -0,0 +1,18 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts(2,5): error TS2322: Type 'string' is not assignable to type '"ABC"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts(3,5): error TS2322: Type 'string' is not assignable to type '"DE\nF"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts(6,5): error TS2322: Type 'string' is not assignable to type '"JK`L"'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts (3 errors) ==== + + let ABC: "ABC" = `ABC`; + ~~~ +!!! error TS2322: Type 'string' is not assignable to type '"ABC"'. + let DE_NEWLINE_F: "DE\nF" = `DE + ~~~~~~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"DE\nF"'. + F`; + let G_QUOTE_HI: 'G"HI'; + let JK_BACKTICK_L: "JK`L" = `JK\`L`; + ~~~~~~~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"JK`L"'. \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.js b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.js new file mode 100644 index 0000000000000..8f548afc7bf19 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings01.js @@ -0,0 +1,20 @@ +//// [stringLiteralTypesWithTemplateStrings01.ts] + +let ABC: "ABC" = `ABC`; +let DE_NEWLINE_F: "DE\nF" = `DE +F`; +let G_QUOTE_HI: 'G"HI'; +let JK_BACKTICK_L: "JK`L" = `JK\`L`; + +//// [stringLiteralTypesWithTemplateStrings01.js] +var ABC = "ABC"; +var DE_NEWLINE_F = "DE\nF"; +var G_QUOTE_HI; +var JK_BACKTICK_L = "JK`L"; + + +//// [stringLiteralTypesWithTemplateStrings01.d.ts] +declare let ABC: "ABC"; +declare let DE_NEWLINE_F: "DE\nF"; +declare let G_QUOTE_HI: 'G"HI'; +declare let JK_BACKTICK_L: "JK`L"; diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.errors.txt b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.errors.txt new file mode 100644 index 0000000000000..803e4f5cb24e7 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.errors.txt @@ -0,0 +1,13 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts(2,5): error TS2322: Type 'string' is not assignable to type '"AB\r\nC"'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts(4,5): error TS2322: Type 'string' is not assignable to type '"DE\nF"'. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts (2 errors) ==== + + let abc: "AB\r\nC" = `AB + ~~~ +!!! error TS2322: Type 'string' is not assignable to type '"AB\r\nC"'. + C`; + let de_NEWLINE_f: "DE\nF" = `DE${"\n"}F`; + ~~~~~~~~~~~~ +!!! error TS2322: Type 'string' is not assignable to type '"DE\nF"'. \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.js b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.js new file mode 100644 index 0000000000000..f5d9045921a8e --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithTemplateStrings02.js @@ -0,0 +1,14 @@ +//// [stringLiteralTypesWithTemplateStrings02.ts] + +let abc: "AB\r\nC" = `AB +C`; +let de_NEWLINE_f: "DE\nF" = `DE${"\n"}F`; + +//// [stringLiteralTypesWithTemplateStrings02.js] +var abc = "AB\nC"; +var de_NEWLINE_f = "DE" + "\n" + "F"; + + +//// [stringLiteralTypesWithTemplateStrings02.d.ts] +declare let abc: "AB\r\nC"; +declare let de_NEWLINE_f: "DE\nF"; diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.js b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.js new file mode 100644 index 0000000000000..1576540e24446 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.js @@ -0,0 +1,86 @@ +//// [stringLiteralTypesWithVariousOperators01.ts] + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = "" + abc; +let b = abc + ""; +let c = 10 + abc; +let d = abc + 10; +let e = xyz + abc; +let f = abc + xyz; +let g = true + abc; +let h = abc + true; +let i = abc + abcOrXyz + xyz; +let j = abcOrXyz + abcOrXyz; +let k = +abcOrXyz; +let l = -abcOrXyz; +let m = abcOrXyzOrNumber + ""; +let n = "" + abcOrXyzOrNumber; +let o = abcOrXyzOrNumber + abcOrXyz; +let p = abcOrXyz + abcOrXyzOrNumber; +let q = !abcOrXyzOrNumber; +let r = ~abcOrXyzOrNumber; +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +let t = abcOrXyzOrNumber >= abcOrXyz; +let u = abc === abcOrXyz; +let v = abcOrXyz === abcOrXyzOrNumber; + +//// [stringLiteralTypesWithVariousOperators01.js] +var abc = "ABC"; +var xyz = "XYZ"; +var abcOrXyz = abc || xyz; +var abcOrXyzOrNumber = abcOrXyz || 100; +var a = "" + abc; +var b = abc + ""; +var c = 10 + abc; +var d = abc + 10; +var e = xyz + abc; +var f = abc + xyz; +var g = true + abc; +var h = abc + true; +var i = abc + abcOrXyz + xyz; +var j = abcOrXyz + abcOrXyz; +var k = +abcOrXyz; +var l = -abcOrXyz; +var m = abcOrXyzOrNumber + ""; +var n = "" + abcOrXyzOrNumber; +var o = abcOrXyzOrNumber + abcOrXyz; +var p = abcOrXyz + abcOrXyzOrNumber; +var q = !abcOrXyzOrNumber; +var r = ~abcOrXyzOrNumber; +var s = abcOrXyzOrNumber < abcOrXyzOrNumber; +var t = abcOrXyzOrNumber >= abcOrXyz; +var u = abc === abcOrXyz; +var v = abcOrXyz === abcOrXyzOrNumber; + + +//// [stringLiteralTypesWithVariousOperators01.d.ts] +declare let abc: "ABC"; +declare let xyz: "XYZ"; +declare let abcOrXyz: "ABC" | "XYZ"; +declare let abcOrXyzOrNumber: "ABC" | "XYZ" | number; +declare let a: string; +declare let b: string; +declare let c: string; +declare let d: string; +declare let e: string; +declare let f: string; +declare let g: string; +declare let h: string; +declare let i: string; +declare let j: string; +declare let k: number; +declare let l: number; +declare let m: string; +declare let n: string; +declare let o: string; +declare let p: string; +declare let q: boolean; +declare let r: number; +declare let s: boolean; +declare let t: boolean; +declare let u: boolean; +declare let v: boolean; diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.symbols b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.symbols new file mode 100644 index 0000000000000..cc9c3aa842bd3 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.symbols @@ -0,0 +1,116 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts === + +let abc: "ABC" = "ABC"; +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let xyz: "XYZ" = "XYZ"; +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let a = "" + abc; +>a : Symbol(a, Decl(stringLiteralTypesWithVariousOperators01.ts, 6, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let b = abc + ""; +>b : Symbol(b, Decl(stringLiteralTypesWithVariousOperators01.ts, 7, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let c = 10 + abc; +>c : Symbol(c, Decl(stringLiteralTypesWithVariousOperators01.ts, 8, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let d = abc + 10; +>d : Symbol(d, Decl(stringLiteralTypesWithVariousOperators01.ts, 9, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let e = xyz + abc; +>e : Symbol(e, Decl(stringLiteralTypesWithVariousOperators01.ts, 10, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let f = abc + xyz; +>f : Symbol(f, Decl(stringLiteralTypesWithVariousOperators01.ts, 11, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let g = true + abc; +>g : Symbol(g, Decl(stringLiteralTypesWithVariousOperators01.ts, 12, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let h = abc + true; +>h : Symbol(h, Decl(stringLiteralTypesWithVariousOperators01.ts, 13, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) + +let i = abc + abcOrXyz + xyz; +>i : Symbol(i, Decl(stringLiteralTypesWithVariousOperators01.ts, 14, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>xyz : Symbol(xyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 2, 3)) + +let j = abcOrXyz + abcOrXyz; +>j : Symbol(j, Decl(stringLiteralTypesWithVariousOperators01.ts, 15, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let k = +abcOrXyz; +>k : Symbol(k, Decl(stringLiteralTypesWithVariousOperators01.ts, 16, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let l = -abcOrXyz; +>l : Symbol(l, Decl(stringLiteralTypesWithVariousOperators01.ts, 17, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let m = abcOrXyzOrNumber + ""; +>m : Symbol(m, Decl(stringLiteralTypesWithVariousOperators01.ts, 18, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let n = "" + abcOrXyzOrNumber; +>n : Symbol(n, Decl(stringLiteralTypesWithVariousOperators01.ts, 19, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let o = abcOrXyzOrNumber + abcOrXyz; +>o : Symbol(o, Decl(stringLiteralTypesWithVariousOperators01.ts, 20, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let p = abcOrXyz + abcOrXyzOrNumber; +>p : Symbol(p, Decl(stringLiteralTypesWithVariousOperators01.ts, 21, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let q = !abcOrXyzOrNumber; +>q : Symbol(q, Decl(stringLiteralTypesWithVariousOperators01.ts, 22, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let r = ~abcOrXyzOrNumber; +>r : Symbol(r, Decl(stringLiteralTypesWithVariousOperators01.ts, 23, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +>s : Symbol(s, Decl(stringLiteralTypesWithVariousOperators01.ts, 24, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + +let t = abcOrXyzOrNumber >= abcOrXyz; +>t : Symbol(t, Decl(stringLiteralTypesWithVariousOperators01.ts, 25, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let u = abc === abcOrXyz; +>u : Symbol(u, Decl(stringLiteralTypesWithVariousOperators01.ts, 26, 3)) +>abc : Symbol(abc, Decl(stringLiteralTypesWithVariousOperators01.ts, 1, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) + +let v = abcOrXyz === abcOrXyzOrNumber; +>v : Symbol(v, Decl(stringLiteralTypesWithVariousOperators01.ts, 27, 3)) +>abcOrXyz : Symbol(abcOrXyz, Decl(stringLiteralTypesWithVariousOperators01.ts, 3, 3)) +>abcOrXyzOrNumber : Symbol(abcOrXyzOrNumber, Decl(stringLiteralTypesWithVariousOperators01.ts, 4, 3)) + diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.types b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.types new file mode 100644 index 0000000000000..90cb538b800d1 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators01.types @@ -0,0 +1,152 @@ +=== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts === + +let abc: "ABC" = "ABC"; +>abc : "ABC" +>"ABC" : "ABC" + +let xyz: "XYZ" = "XYZ"; +>xyz : "XYZ" +>"XYZ" : "XYZ" + +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +>abcOrXyz : "ABC" | "XYZ" +>abc || xyz : "ABC" | "XYZ" +>abc : "ABC" +>xyz : "XYZ" + +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyz || 100 : "ABC" | "XYZ" | number +>abcOrXyz : "ABC" | "XYZ" +>100 : number + +let a = "" + abc; +>a : string +>"" + abc : string +>"" : string +>abc : "ABC" + +let b = abc + ""; +>b : string +>abc + "" : string +>abc : "ABC" +>"" : string + +let c = 10 + abc; +>c : string +>10 + abc : string +>10 : number +>abc : "ABC" + +let d = abc + 10; +>d : string +>abc + 10 : string +>abc : "ABC" +>10 : number + +let e = xyz + abc; +>e : string +>xyz + abc : string +>xyz : "XYZ" +>abc : "ABC" + +let f = abc + xyz; +>f : string +>abc + xyz : string +>abc : "ABC" +>xyz : "XYZ" + +let g = true + abc; +>g : string +>true + abc : string +>true : boolean +>abc : "ABC" + +let h = abc + true; +>h : string +>abc + true : string +>abc : "ABC" +>true : boolean + +let i = abc + abcOrXyz + xyz; +>i : string +>abc + abcOrXyz + xyz : string +>abc + abcOrXyz : string +>abc : "ABC" +>abcOrXyz : "ABC" | "XYZ" +>xyz : "XYZ" + +let j = abcOrXyz + abcOrXyz; +>j : string +>abcOrXyz + abcOrXyz : string +>abcOrXyz : "ABC" | "XYZ" +>abcOrXyz : "ABC" | "XYZ" + +let k = +abcOrXyz; +>k : number +>+abcOrXyz : number +>abcOrXyz : "ABC" | "XYZ" + +let l = -abcOrXyz; +>l : number +>-abcOrXyz : number +>abcOrXyz : "ABC" | "XYZ" + +let m = abcOrXyzOrNumber + ""; +>m : string +>abcOrXyzOrNumber + "" : string +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>"" : string + +let n = "" + abcOrXyzOrNumber; +>n : string +>"" + abcOrXyzOrNumber : string +>"" : string +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let o = abcOrXyzOrNumber + abcOrXyz; +>o : string +>abcOrXyzOrNumber + abcOrXyz : string +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyz : "ABC" | "XYZ" + +let p = abcOrXyz + abcOrXyzOrNumber; +>p : string +>abcOrXyz + abcOrXyzOrNumber : string +>abcOrXyz : "ABC" | "XYZ" +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let q = !abcOrXyzOrNumber; +>q : boolean +>!abcOrXyzOrNumber : boolean +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let r = ~abcOrXyzOrNumber; +>r : number +>~abcOrXyzOrNumber : number +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +>s : boolean +>abcOrXyzOrNumber < abcOrXyzOrNumber : boolean +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + +let t = abcOrXyzOrNumber >= abcOrXyz; +>t : boolean +>abcOrXyzOrNumber >= abcOrXyz : boolean +>abcOrXyzOrNumber : "ABC" | "XYZ" | number +>abcOrXyz : "ABC" | "XYZ" + +let u = abc === abcOrXyz; +>u : boolean +>abc === abcOrXyz : boolean +>abc : "ABC" +>abcOrXyz : "ABC" | "XYZ" + +let v = abcOrXyz === abcOrXyzOrNumber; +>v : boolean +>abcOrXyz === abcOrXyzOrNumber : boolean +>abcOrXyz : "ABC" | "XYZ" +>abcOrXyzOrNumber : "ABC" | "XYZ" | number + diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt new file mode 100644 index 0000000000000..69dd6c2f6a955 --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.errors.txt @@ -0,0 +1,48 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(7,9): error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(8,9): error TS2365: Operator '+' cannot be applied to types 'number' and '"ABC" | "XYZ" | number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(9,9): error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and '"ABC" | "XYZ" | number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(10,9): error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'boolean'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(11,9): error TS2365: Operator '+' cannot be applied to types 'boolean' and '"ABC" | "XYZ" | number'. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(12,9): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(13,11): error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(14,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. +tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts(15,9): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + + +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts (9 errors) ==== + + let abc: "ABC" = "ABC"; + let xyz: "XYZ" = "XYZ"; + let abcOrXyz: "ABC" | "XYZ" = abc || xyz; + let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + + let a = abcOrXyzOrNumber + 100; + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'number'. + let b = 100 + abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types 'number' and '"ABC" | "XYZ" | number'. + let c = abcOrXyzOrNumber + abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and '"ABC" | "XYZ" | number'. + let d = abcOrXyzOrNumber + true; + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types '"ABC" | "XYZ" | number' and 'boolean'. + let e = false + abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2365: Operator '+' cannot be applied to types 'boolean' and '"ABC" | "XYZ" | number'. + let f = abcOrXyzOrNumber++; + ~~~~~~~~~~~~~~~~ +!!! error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. + let g = --abcOrXyzOrNumber; + ~~~~~~~~~~~~~~~~ +!!! error TS2356: An arithmetic operand must be of type 'any', 'number' or an enum type. + let h = abcOrXyzOrNumber ^ 10; + ~~~~~~~~~~~~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + let i = abcOrXyzOrNumber | 10; + ~~~~~~~~~~~~~~~~ +!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. + let j = abc < xyz; + let k = abc === xyz; + let l = abc != xyz; \ No newline at end of file diff --git a/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.js b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.js new file mode 100644 index 0000000000000..4914ecf3d178a --- /dev/null +++ b/tests/baselines/reference/stringLiteralTypesWithVariousOperators02.js @@ -0,0 +1,56 @@ +//// [stringLiteralTypesWithVariousOperators02.ts] + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = abcOrXyzOrNumber + 100; +let b = 100 + abcOrXyzOrNumber; +let c = abcOrXyzOrNumber + abcOrXyzOrNumber; +let d = abcOrXyzOrNumber + true; +let e = false + abcOrXyzOrNumber; +let f = abcOrXyzOrNumber++; +let g = --abcOrXyzOrNumber; +let h = abcOrXyzOrNumber ^ 10; +let i = abcOrXyzOrNumber | 10; +let j = abc < xyz; +let k = abc === xyz; +let l = abc != xyz; + +//// [stringLiteralTypesWithVariousOperators02.js] +var abc = "ABC"; +var xyz = "XYZ"; +var abcOrXyz = abc || xyz; +var abcOrXyzOrNumber = abcOrXyz || 100; +var a = abcOrXyzOrNumber + 100; +var b = 100 + abcOrXyzOrNumber; +var c = abcOrXyzOrNumber + abcOrXyzOrNumber; +var d = abcOrXyzOrNumber + true; +var e = false + abcOrXyzOrNumber; +var f = abcOrXyzOrNumber++; +var g = --abcOrXyzOrNumber; +var h = abcOrXyzOrNumber ^ 10; +var i = abcOrXyzOrNumber | 10; +var j = abc < xyz; +var k = abc === xyz; +var l = abc != xyz; + + +//// [stringLiteralTypesWithVariousOperators02.d.ts] +declare let abc: "ABC"; +declare let xyz: "XYZ"; +declare let abcOrXyz: "ABC" | "XYZ"; +declare let abcOrXyzOrNumber: "ABC" | "XYZ" | number; +declare let a: any; +declare let b: any; +declare let c: any; +declare let d: any; +declare let e: any; +declare let f: number; +declare let g: number; +declare let h: number; +declare let i: number; +declare let j: boolean; +declare let k: boolean; +declare let l: boolean; diff --git a/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt b/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt index d3c399477d66c..da742fcd69cbd 100644 --- a/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt +++ b/tests/baselines/reference/subtypingWithCallSignaturesWithSpecializedSignatures.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithCallSignaturesWithSpecializedSignatures.ts(70,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. Types of property 'a' are incompatible. - Type '(x: string) => string' is not assignable to type '{ (x: 'a'): number; (x: string): number; }'. + Type '(x: string) => string' is not assignable to type '{ (x: "a"): number; (x: string): number; }'. Type 'string' is not assignable to type 'number'. @@ -78,7 +78,7 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. !!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type '(x: string) => string' is not assignable to type '{ (x: 'a'): number; (x: string): number; }'. +!!! error TS2430: Type '(x: string) => string' is not assignable to type '{ (x: "a"): number; (x: string): number; }'. !!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: (x: string) => string; // error because base returns non-void; diff --git a/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt b/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt index 03cc6de0ca4b4..8a885a2455fb2 100644 --- a/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt +++ b/tests/baselines/reference/subtypingWithConstructSignaturesWithSpecializedSignatures.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithConstructSignaturesWithSpecializedSignatures.ts(70,15): error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. Types of property 'a' are incompatible. - Type 'new (x: string) => string' is not assignable to type '{ new (x: 'a'): number; new (x: string): number; }'. + Type 'new (x: string) => string' is not assignable to type '{ new (x: "a"): number; new (x: string): number; }'. Type 'string' is not assignable to type 'number'. @@ -78,7 +78,7 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW ~~ !!! error TS2430: Interface 'I2' incorrectly extends interface 'Base2'. !!! error TS2430: Types of property 'a' are incompatible. -!!! error TS2430: Type 'new (x: string) => string' is not assignable to type '{ new (x: 'a'): number; new (x: string): number; }'. +!!! error TS2430: Type 'new (x: string) => string' is not assignable to type '{ new (x: "a"): number; new (x: string): number; }'. !!! error TS2430: Type 'string' is not assignable to type 'number'. // N's a: new (x: string) => string; // error because base returns non-void; diff --git a/tests/baselines/reference/symbolProperty41.types b/tests/baselines/reference/symbolProperty41.types index 61b71dfe24689..f312481ac0403 100644 --- a/tests/baselines/reference/symbolProperty41.types +++ b/tests/baselines/reference/symbolProperty41.types @@ -49,5 +49,5 @@ c[Symbol.iterator]("hello"); >Symbol.iterator : symbol >Symbol : SymbolConstructor >iterator : symbol ->"hello" : string +>"hello" : "hello" diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt new file mode 100644 index 0000000000000..49f17e7891de1 --- /dev/null +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.errors.txt @@ -0,0 +1,212 @@ +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(4,18): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(37,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(38,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(39,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(40,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(41,25): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(44,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(45,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(46,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(47,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(48,30): error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(55,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(57,52): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(58,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(61,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(63,5): error TS2322: Type 'string' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(75,5): error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(77,5): error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(87,43): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(88,43): error TS2345: Argument of type '"Hello"' is not assignable to parameter of type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(89,52): error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(93,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(97,5): error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. + Type 'string' is not assignable to type '"World"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(100,25): error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. +tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts(104,25): error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. + Type '"World"' is not assignable to type '"Hello"'. + + +==== tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts (25 errors) ==== + + declare function randBool(): boolean; + declare function takeReturnString(str: string): string; + declare function takeReturnHello(str: "Hello"): "Hello"; + ~~~~~~~~~~~~~~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; + + function fun1(x: T, y: T) { + return randBool() ? x : y; + } + + function fun2(x: T, y: U) { + return randBool() ? x : y; + } + + function fun3(...args: T[]): T { + return args[+randBool()]; + } + + namespace n1 { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + export let a = fun1("Hello", "World"); + export let b = fun1("Hello", "Hello"); + export let c = fun2("Hello", "World"); + export let d = fun2("Hello", "Hello"); + export let e = fun3("Hello", "Hello", "World", "Foo"); + + // Should be valid + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + b = takeReturnHello(b); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + c = takeReturnHello(c); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + d = takeReturnHello(d); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + e = takeReturnHello(e); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello"'. + + // Passing these as arguments should cause an error. + a = takeReturnHelloWorld(a); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. + b = takeReturnHelloWorld(b); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. + c = takeReturnHelloWorld(c); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. + d = takeReturnHelloWorld(d); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. + e = takeReturnHelloWorld(e); + ~ +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '"Hello" | "World"'. +!!! error TS2345: Type 'string' is not assignable to type '"World"'. + } + + namespace n2 { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + export let a = fun1<"Hello">("Hello", "Hello"); + export let b = fun1<"Hello">("Hello", "World"); + ~~~~~~~ +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. + export let c = fun2<"Hello", "Hello">("Hello", "Hello"); + export let d = fun2<"Hello", "Hello">("Hello", "World"); + ~~~~~~~ +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. + export let e = fun3<"Hello">("Hello", "World"); + ~~~~~~~ +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello"'. + b = takeReturnString(b); + c = takeReturnString(c); + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello"'. + d = takeReturnString(d); + e = takeReturnString(e); + + // Should be valid + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Assignment from the returned value should cause an error. + a = takeReturnHelloWorld(a); + ~ +!!! error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. +!!! error TS2322: Type '"World"' is not assignable to type '"Hello"'. + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + ~ +!!! error TS2322: Type '"Hello" | "World"' is not assignable to type '"Hello"'. +!!! error TS2322: Type '"World"' is not assignable to type '"Hello"'. + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); + } + + + namespace n3 { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + export let a = fun2<"Hello", "World">("Hello", "World"); + export let b = fun2<"Hello", "World">("World", "Hello"); + ~~~~~~~ +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. + export let c = fun2<"World", "Hello">("Hello", "Hello"); + ~~~~~~~ +!!! error TS2345: Argument of type '"Hello"' is not assignable to parameter of type '"World"'. + export let d = fun2<"World", "Hello">("World", "World"); + ~~~~~~~ +!!! error TS2345: Argument of type '"World"' is not assignable to parameter of type '"Hello"'. + export let e = fun3<"Hello" | "World">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. +!!! error TS2322: Type 'string' is not assignable to type '"World"'. + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + ~ +!!! error TS2322: Type 'string' is not assignable to type '"Hello" | "World"'. +!!! error TS2322: Type 'string' is not assignable to type '"World"'. + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + ~ +!!! error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Type '"World"' is not assignable to type '"Hello"'. + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + ~ +!!! error TS2345: Argument of type '"Hello" | "World"' is not assignable to parameter of type '"Hello"'. +!!! error TS2345: Type '"World"' is not assignable to type '"Hello"'. + + // Both should be valid. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); + } \ No newline at end of file diff --git a/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js new file mode 100644 index 0000000000000..c07dd22539648 --- /dev/null +++ b/tests/baselines/reference/typeArgumentsWithStringLiteralTypes01.js @@ -0,0 +1,244 @@ +//// [typeArgumentsWithStringLiteralTypes01.ts] + +declare function randBool(): boolean; +declare function takeReturnString(str: string): string; +declare function takeReturnHello(str: "Hello"): "Hello"; +declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; + +function fun1(x: T, y: T) { + return randBool() ? x : y; +} + +function fun2(x: T, y: U) { + return randBool() ? x : y; +} + +function fun3(...args: T[]): T { + return args[+randBool()]; +} + +namespace n1 { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + export let a = fun1("Hello", "World"); + export let b = fun1("Hello", "Hello"); + export let c = fun2("Hello", "World"); + export let d = fun2("Hello", "Hello"); + export let e = fun3("Hello", "Hello", "World", "Foo"); + + // Should be valid + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Passing these as arguments should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + +namespace n2 { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + export let a = fun1<"Hello">("Hello", "Hello"); + export let b = fun1<"Hello">("Hello", "World"); + export let c = fun2<"Hello", "Hello">("Hello", "Hello"); + export let d = fun2<"Hello", "Hello">("Hello", "World"); + export let e = fun3<"Hello">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Should be valid + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Assignment from the returned value should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + + +namespace n3 { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + export let a = fun2<"Hello", "World">("Hello", "World"); + export let b = fun2<"Hello", "World">("World", "Hello"); + export let c = fun2<"World", "Hello">("Hello", "Hello"); + export let d = fun2<"World", "Hello">("World", "World"); + export let e = fun3<"Hello" | "World">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Both should be valid. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + +//// [typeArgumentsWithStringLiteralTypes01.js] +function fun1(x, y) { + return randBool() ? x : y; +} +function fun2(x, y) { + return randBool() ? x : y; +} +function fun3() { + var args = []; + for (var _i = 0; _i < arguments.length; _i++) { + args[_i - 0] = arguments[_i]; + } + return args[+randBool()]; +} +var n1; +(function (n1) { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + n1.a = fun1("Hello", "World"); + n1.b = fun1("Hello", "Hello"); + n1.c = fun2("Hello", "World"); + n1.d = fun2("Hello", "Hello"); + n1.e = fun3("Hello", "Hello", "World", "Foo"); + // Should be valid + n1.a = takeReturnString(n1.a); + n1.b = takeReturnString(n1.b); + n1.c = takeReturnString(n1.c); + n1.d = takeReturnString(n1.d); + n1.e = takeReturnString(n1.e); + // Passing these as arguments should cause an error. + n1.a = takeReturnHello(n1.a); + n1.b = takeReturnHello(n1.b); + n1.c = takeReturnHello(n1.c); + n1.d = takeReturnHello(n1.d); + n1.e = takeReturnHello(n1.e); + // Passing these as arguments should cause an error. + n1.a = takeReturnHelloWorld(n1.a); + n1.b = takeReturnHelloWorld(n1.b); + n1.c = takeReturnHelloWorld(n1.c); + n1.d = takeReturnHelloWorld(n1.d); + n1.e = takeReturnHelloWorld(n1.e); +})(n1 || (n1 = {})); +var n2; +(function (n2) { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + n2.a = fun1("Hello", "Hello"); + n2.b = fun1("Hello", "World"); + n2.c = fun2("Hello", "Hello"); + n2.d = fun2("Hello", "World"); + n2.e = fun3("Hello", "World"); + // Assignment from the returned value should cause an error. + n2.a = takeReturnString(n2.a); + n2.b = takeReturnString(n2.b); + n2.c = takeReturnString(n2.c); + n2.d = takeReturnString(n2.d); + n2.e = takeReturnString(n2.e); + // Should be valid + n2.a = takeReturnHello(n2.a); + n2.b = takeReturnHello(n2.b); + n2.c = takeReturnHello(n2.c); + n2.d = takeReturnHello(n2.d); + n2.e = takeReturnHello(n2.e); + // Assignment from the returned value should cause an error. + n2.a = takeReturnHelloWorld(n2.a); + n2.b = takeReturnHelloWorld(n2.b); + n2.c = takeReturnHelloWorld(n2.c); + n2.d = takeReturnHelloWorld(n2.d); + n2.e = takeReturnHelloWorld(n2.e); +})(n2 || (n2 = {})); +var n3; +(function (n3) { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + n3.a = fun2("Hello", "World"); + n3.b = fun2("World", "Hello"); + n3.c = fun2("Hello", "Hello"); + n3.d = fun2("World", "World"); + n3.e = fun3("Hello", "World"); + // Assignment from the returned value should cause an error. + n3.a = takeReturnString(n3.a); + n3.b = takeReturnString(n3.b); + n3.c = takeReturnString(n3.c); + n3.d = takeReturnString(n3.d); + n3.e = takeReturnString(n3.e); + // Passing these as arguments should cause an error. + n3.a = takeReturnHello(n3.a); + n3.b = takeReturnHello(n3.b); + n3.c = takeReturnHello(n3.c); + n3.d = takeReturnHello(n3.d); + n3.e = takeReturnHello(n3.e); + // Both should be valid. + n3.a = takeReturnHelloWorld(n3.a); + n3.b = takeReturnHelloWorld(n3.b); + n3.c = takeReturnHelloWorld(n3.c); + n3.d = takeReturnHelloWorld(n3.d); + n3.e = takeReturnHelloWorld(n3.e); +})(n3 || (n3 = {})); + + +//// [typeArgumentsWithStringLiteralTypes01.d.ts] +declare function randBool(): boolean; +declare function takeReturnString(str: string): string; +declare function takeReturnHello(str: "Hello"): "Hello"; +declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; +declare function fun1(x: T, y: T): T; +declare function fun2(x: T, y: U): T | U; +declare function fun3(...args: T[]): T; +declare namespace n1 { + let a: string; + let b: string; + let c: string; + let d: string; + let e: string; +} +declare namespace n2 { + let a: "Hello"; + let b: any; + let c: "Hello"; + let d: any; + let e: any; +} +declare namespace n3 { + let a: "Hello" | "World"; + let b: any; + let c: any; + let d: any; + let e: "Hello" | "World"; +} diff --git a/tests/baselines/reference/typeParameterConstraints1.errors.txt b/tests/baselines/reference/typeParameterConstraints1.errors.txt index f30fd4f6700b4..e837e19317deb 100644 --- a/tests/baselines/reference/typeParameterConstraints1.errors.txt +++ b/tests/baselines/reference/typeParameterConstraints1.errors.txt @@ -1,12 +1,11 @@ tests/cases/compiler/typeParameterConstraints1.ts(6,25): error TS2304: Cannot find name 'hm'. -tests/cases/compiler/typeParameterConstraints1.ts(8,25): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(9,25): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(10,26): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(11,26): error TS1110: Type expected. tests/cases/compiler/typeParameterConstraints1.ts(12,26): error TS2304: Cannot find name 'undefined'. -==== tests/cases/compiler/typeParameterConstraints1.ts (6 errors) ==== +==== tests/cases/compiler/typeParameterConstraints1.ts (5 errors) ==== function foo1(test: T) { } function foo2(test: T) { } function foo3(test: T) { } @@ -17,8 +16,6 @@ tests/cases/compiler/typeParameterConstraints1.ts(12,26): error TS2304: Cannot f !!! error TS2304: Cannot find name 'hm'. function foo7(test: T) { } // valid function foo8(test: T) { } - ~~ -!!! error TS1110: Type expected. function foo9 (test: T) { } ~ !!! error TS1110: Type expected. diff --git a/tests/baselines/reference/typesWithSpecializedCallSignatures.types b/tests/baselines/reference/typesWithSpecializedCallSignatures.types index fa3ac7a4ba4d4..b587ee7840e27 100644 --- a/tests/baselines/reference/typesWithSpecializedCallSignatures.types +++ b/tests/baselines/reference/typesWithSpecializedCallSignatures.types @@ -19,22 +19,22 @@ class C { >C : C foo(x: 'hi'): Derived1; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'hi' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "hi" >Derived1 : Derived1 foo(x: 'bye'): Derived2; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'bye' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "bye" >Derived2 : Derived2 foo(x: string): Base; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : string >Base : Base foo(x) { ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : any return x; @@ -50,17 +50,17 @@ interface I { >I : I foo(x: 'hi'): Derived1; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'hi' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "hi" >Derived1 : Derived1 foo(x: 'bye'): Derived2; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'bye' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "bye" >Derived2 : Derived2 foo(x: string): Base; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : string >Base : Base } @@ -69,20 +69,20 @@ var i: I; >I : I var a: { ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } foo(x: 'hi'): Derived1; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'hi' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "hi" >Derived1 : Derived1 foo(x: 'bye'): Derived2; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->x : 'bye' +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>x : "bye" >Derived2 : Derived2 foo(x: string): Base; ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >x : string >Base : Base @@ -94,9 +94,9 @@ c = i; >i : I c = a; ->c = a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>c = a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >c : C ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } i = c; >i = c : C @@ -104,44 +104,44 @@ i = c; >c : C i = a; ->i = a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>i = a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >i : I ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } a = c; >a = c : C ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >c : C a = i; >a = i : I ->a : { foo(x: 'hi'): Derived1; foo(x: 'bye'): Derived2; foo(x: string): Base; } +>a : { foo(x: "hi"): Derived1; foo(x: "bye"): Derived2; foo(x: string): Base; } >i : I var r1: Derived1 = c.foo('hi'); >r1 : Derived1 >Derived1 : Derived1 >c.foo('hi') : Derived1 ->c.foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->'hi' : string +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>'hi' : "hi" var r2: Derived2 = c.foo('bye'); >r2 : Derived2 >Derived2 : Derived2 >c.foo('bye') : Derived2 ->c.foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } ->'bye' : string +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } +>'bye' : "bye" var r3: Base = c.foo('hm'); >r3 : Base >Base : Base >c.foo('hm') : Base ->c.foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>c.foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >c : C ->foo : { (x: 'hi'): Derived1; (x: 'bye'): Derived2; (x: string): Base; } +>foo : { (x: "hi"): Derived1; (x: "bye"): Derived2; (x: string): Base; } >'hm' : string diff --git a/tests/baselines/reference/typesWithSpecializedConstructSignatures.types b/tests/baselines/reference/typesWithSpecializedConstructSignatures.types index 66e274d917adc..3036c0cea714d 100644 --- a/tests/baselines/reference/typesWithSpecializedConstructSignatures.types +++ b/tests/baselines/reference/typesWithSpecializedConstructSignatures.types @@ -19,10 +19,10 @@ class C { >C : C constructor(x: 'hi'); ->x : 'hi' +>x : "hi" constructor(x: 'bye'); ->x : 'bye' +>x : "bye" constructor(x: string); >x : string @@ -44,11 +44,11 @@ interface I { >I : I new(x: 'hi'): Derived1; ->x : 'hi' +>x : "hi" >Derived1 : Derived1 new(x: 'bye'): Derived2; ->x : 'bye' +>x : "bye" >Derived2 : Derived2 new(x: string): Base; @@ -60,14 +60,14 @@ var i: I; >I : I var a: { ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } new(x: 'hi'): Derived1; ->x : 'hi' +>x : "hi" >Derived1 : Derived1 new(x: 'bye'): Derived2; ->x : 'bye' +>x : "bye" >Derived2 : Derived2 new(x: string): Base; @@ -82,37 +82,37 @@ c = i; >i : I c = a; ->c = a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>c = a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >c : C ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } i = a; ->i = a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>i = a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >i : I ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } a = i; >a = i : I ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >i : I var r1 = new C('hi'); >r1 : C >new C('hi') : C >C : typeof C ->'hi' : string +>'hi' : "hi" var r2: Derived2 = new i('bye'); >r2 : Derived2 >Derived2 : Derived2 >new i('bye') : Derived2 >i : I ->'bye' : string +>'bye' : "bye" var r3: Base = new a('hm'); >r3 : Base >Base : Base >new a('hm') : Base ->a : { new (x: 'hi'): Derived1; new (x: 'bye'): Derived2; new (x: string): Base; } +>a : { new (x: "hi"): Derived1; new (x: "bye"): Derived2; new (x: string): Base; } >'hm' : string diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts new file mode 100644 index 0000000000000..f13ce35e82914 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf01.ts @@ -0,0 +1,15 @@ + +type S = "a" | "b"; +type T = S[] | S; + +function f(foo: T) { + if (foo === "a") { + return foo; + } + else if (foo === "b") { + return foo; + } + else { + return (foo as S[])[0]; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts new file mode 100644 index 0000000000000..4eaf18ca7ccb4 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralCheckedInIf02.ts @@ -0,0 +1,16 @@ + +type S = "a" | "b"; +type T = S[] | S; + +function isS(t: T): t is S { + return t === "a" || t === "b"; +} + +function f(foo: T) { + if (isS(foo)) { + return foo; + } + else { + return foo[0]; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts new file mode 100644 index 0000000000000..141f5d12795ef --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralMatchedInSwitch01.ts @@ -0,0 +1,13 @@ + +type S = "a" | "b"; +type T = S[] | S; + +var foo: T; +switch (foo) { + case "a": + case "b": + break; + default: + foo = (foo as S[])[0]; + break; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts new file mode 100644 index 0000000000000..ec1f1e5eb5634 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypeAssertion01.ts @@ -0,0 +1,31 @@ + +type S = "a" | "b"; +type T = S[] | S; + +var s: S; +var t: T; +var str: string; + +//////////////// + +s = t; +s = t as S; + +s = str; +s = str as S; + +//////////////// + +t = s; +t = s as T; + +t = str; +t = str as T; + +//////////////// + +str = s; +str = s as string; + +str = t; +str = t as string; diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts new file mode 100644 index 0000000000000..388f4567ed72a --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndTuples01.ts @@ -0,0 +1,20 @@ +// @declaration: true + +// Should all be strings. +let [hello, brave, newish, world] = ["Hello", "Brave", "New", "World"]; + +type RexOrRaptor = "t-rex" | "raptor" +let [im, a, dinosaur]: ["I'm", "a", RexOrRaptor] = ['I\'m', 'a', 't-rex']; + +rawr(dinosaur); + +function rawr(dino: RexOrRaptor) { + if (dino === "t-rex") { + return "ROAAAAR!"; + } + if (dino === "raptor") { + return "yip yip!"; + } + + throw "Unexpected " + dino; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts new file mode 100644 index 0000000000000..cea04f0818e75 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts @@ -0,0 +1,43 @@ +// @declaration: true + +type Kind = "A" | "B" + +interface Entity { + kind: Kind; +} + +interface A extends Entity { + kind: "A"; + a: number; +} + +interface B extends Entity { + kind: "B"; + b: string; +} + +function hasKind(entity: Entity, kind: "A"): entity is A; +function hasKind(entity: Entity, kind: "B"): entity is B; +function hasKind(entity: Entity, kind: Kind): entity is Entity; +function hasKind(entity: Entity, kind: Kind): boolean { + return kind === is; +} + +let x: A = { + kind: "A", + a: 100, +} + +if (hasKind(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!hasKind(x, "B")) { + let c = x; +} +else { + let d = x; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts new file mode 100644 index 0000000000000..b8ed1b47dd404 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes01.ts @@ -0,0 +1,21 @@ +// @declaration: true + +type T = "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts new file mode 100644 index 0000000000000..2cc63ab44605d --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes02.ts @@ -0,0 +1,21 @@ +// @declaration: true + +type T = string | "foo" | "bar" | "baz"; + +var x: "foo" | "bar" | "baz" | string = "foo"; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts new file mode 100644 index 0000000000000..d5c6a38af799c --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes03.ts @@ -0,0 +1,21 @@ +// @declaration: true + +type T = number | "foo" | "bar"; + +var x: "foo" | "bar" | number; +var y: T = "bar"; + +if (x === "foo") { + let a = x; +} +else if (x !== "bar") { + let b = x || y; +} +else { + let c = x; + let d = y; + let e: (typeof x) | (typeof y) = c || d; +} + +x = y; +y = x; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts new file mode 100644 index 0000000000000..e9d062b0e5c20 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInUnionTypes04.ts @@ -0,0 +1,38 @@ +// @declaration: true + +type T = "" | "foo"; + +let x: T = ""; +let y: T = "foo"; + +if (x === "") { + let a = x; +} + +if (x !== "") { + let b = x; +} + +if (x == "") { + let c = x; +} + +if (x != "") { + let d = x; +} + +if (x) { + let e = x; +} + +if (!x) { + let f = x; +} + +if (!!x) { + let g = x; +} + +if (!!!x) { + let h = x; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts new file mode 100644 index 0000000000000..4f0063876876a --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesInVariableDeclarations01.ts @@ -0,0 +1,19 @@ +// @declaration: true + +let a: ""; +var b: "foo"; +let c: "bar"; +const d: "baz"; + +a = ""; +b = "foo"; +c = "bar"; + +let e: "" = ""; +var f: "foo" = "foo"; +let g: "bar" = "bar"; +const h: "baz" = "baz"; + +e = ""; +f = "foo"; +g = "bar"; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts new file mode 100644 index 0000000000000..d88167eaeffe7 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts @@ -0,0 +1,53 @@ +// @declaration: true + +type PrimitiveName = 'string' | 'number' | 'boolean'; + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: PrimitiveName) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts new file mode 100644 index 0000000000000..5801aa50076b2 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts @@ -0,0 +1,51 @@ +// @declaration: true + +function getFalsyPrimitive(x: "string"): string; +function getFalsyPrimitive(x: "number"): number; +function getFalsyPrimitive(x: "boolean"): boolean; +function getFalsyPrimitive(x: "boolean" | "string"): boolean | string; +function getFalsyPrimitive(x: "boolean" | "number"): boolean | number; +function getFalsyPrimitive(x: "number" | "string"): number | string; +function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean; +function getFalsyPrimitive(x: string) { + if (x === "string") { + return ""; + } + if (x === "number") { + return 0; + } + if (x === "boolean") { + return false; + } + + // Should be unreachable. + throw "Invalid value"; +} + +namespace Consts1 { + const EMPTY_STRING = getFalsyPrimitive("string"); + const ZERO = getFalsyPrimitive('number'); + const FALSE = getFalsyPrimitive("boolean"); +} + +const string: "string" = "string" +const number: "number" = "number" +const boolean: "boolean" = "boolean" + +const stringOrNumber = string || number; +const stringOrBoolean = string || boolean; +const booleanOrNumber = number || boolean; +const stringOrBooleanOrNumber = stringOrBoolean || number; + +namespace Consts2 { + const EMPTY_STRING = getFalsyPrimitive(string); + const ZERO = getFalsyPrimitive(number); + const FALSE = getFalsyPrimitive(boolean); + + const a = getFalsyPrimitive(stringOrNumber); + const b = getFalsyPrimitive(stringOrBoolean); + const c = getFalsyPrimitive(booleanOrNumber); + const d = getFalsyPrimitive(stringOrBooleanOrNumber); +} + + diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts new file mode 100644 index 0000000000000..a675c2c0eaed7 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesOverloads03.ts @@ -0,0 +1,46 @@ +// @declaration: true + +interface Base { + x: string; + y: number; +} + +interface HelloOrWorld extends Base { + p1: boolean; +} + +interface JustHello extends Base { + p2: boolean; +} + +interface JustWorld extends Base { + p3: boolean; +} + +let hello: "hello"; +let world: "world"; +let helloOrWorld: "hello" | "world"; + +function f(p: "hello"): JustHello; +function f(p: "hello" | "world"): HelloOrWorld; +function f(p: "world"): JustWorld; +function f(p: string): Base; +function f(...args: any[]): any { + return undefined; +} + +let fResult1 = f(hello); +let fResult2 = f(world); +let fResult3 = f(helloOrWorld); + +function g(p: string): Base; +function g(p: "hello"): JustHello; +function g(p: "hello" | "world"): HelloOrWorld; +function g(p: "world"): JustWorld; +function g(...args: any[]): any { + return undefined; +} + +let gResult1 = g(hello); +let gResult2 = g(world); +let gResult3 = g(helloOrWorld); \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts new file mode 100644 index 0000000000000..a78918c8122be --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesTypePredicates01.ts @@ -0,0 +1,25 @@ +// @declaration: true + +type Kind = "A" | "B" + +function kindIs(kind: Kind, is: "A"): kind is "A"; +function kindIs(kind: Kind, is: "B"): kind is "B"; +function kindIs(kind: Kind, is: Kind): boolean { + return kind === is; +} + +var x: Kind = "A"; + +if (kindIs(x, "A")) { + let a = x; +} +else { + let b = x; +} + +if (!kindIs(x, "B")) { + let c = x; +} +else { + let d = x; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts new file mode 100644 index 0000000000000..1920551abd812 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings01.ts @@ -0,0 +1,7 @@ +// @declaration: true + +let ABC: "ABC" = `ABC`; +let DE_NEWLINE_F: "DE\nF" = `DE +F`; +let G_QUOTE_HI: 'G"HI'; +let JK_BACKTICK_L: "JK`L" = `JK\`L`; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts new file mode 100644 index 0000000000000..12debae4e2030 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithTemplateStrings02.ts @@ -0,0 +1,5 @@ +// @declaration: true + +let abc: "AB\r\nC" = `AB +C`; +let de_NEWLINE_f: "DE\nF" = `DE${"\n"}F`; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts new file mode 100644 index 0000000000000..28c34d3c74a99 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators01.ts @@ -0,0 +1,29 @@ +// @declaration: true + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = "" + abc; +let b = abc + ""; +let c = 10 + abc; +let d = abc + 10; +let e = xyz + abc; +let f = abc + xyz; +let g = true + abc; +let h = abc + true; +let i = abc + abcOrXyz + xyz; +let j = abcOrXyz + abcOrXyz; +let k = +abcOrXyz; +let l = -abcOrXyz; +let m = abcOrXyzOrNumber + ""; +let n = "" + abcOrXyzOrNumber; +let o = abcOrXyzOrNumber + abcOrXyz; +let p = abcOrXyz + abcOrXyzOrNumber; +let q = !abcOrXyzOrNumber; +let r = ~abcOrXyzOrNumber; +let s = abcOrXyzOrNumber < abcOrXyzOrNumber; +let t = abcOrXyzOrNumber >= abcOrXyz; +let u = abc === abcOrXyz; +let v = abcOrXyz === abcOrXyzOrNumber; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts new file mode 100644 index 0000000000000..cf66f66e47c30 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/stringLiteralTypesWithVariousOperators02.ts @@ -0,0 +1,19 @@ +// @declaration: true + +let abc: "ABC" = "ABC"; +let xyz: "XYZ" = "XYZ"; +let abcOrXyz: "ABC" | "XYZ" = abc || xyz; +let abcOrXyzOrNumber: "ABC" | "XYZ" | number = abcOrXyz || 100; + +let a = abcOrXyzOrNumber + 100; +let b = 100 + abcOrXyzOrNumber; +let c = abcOrXyzOrNumber + abcOrXyzOrNumber; +let d = abcOrXyzOrNumber + true; +let e = false + abcOrXyzOrNumber; +let f = abcOrXyzOrNumber++; +let g = --abcOrXyzOrNumber; +let h = abcOrXyzOrNumber ^ 10; +let i = abcOrXyzOrNumber | 10; +let j = abc < xyz; +let k = abc === xyz; +let l = abc != xyz; \ No newline at end of file diff --git a/tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts b/tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts new file mode 100644 index 0000000000000..08c971458e354 --- /dev/null +++ b/tests/cases/conformance/types/stringLiteral/typeArgumentsWithStringLiteralTypes01.ts @@ -0,0 +1,113 @@ +// @declaration: true + +declare function randBool(): boolean; +declare function takeReturnString(str: string): string; +declare function takeReturnHello(str: "Hello"): "Hello"; +declare function takeReturnHelloWorld(str: "Hello" | "World"): "Hello" | "World"; + +function fun1(x: T, y: T) { + return randBool() ? x : y; +} + +function fun2(x: T, y: U) { + return randBool() ? x : y; +} + +function fun3(...args: T[]): T { + return args[+randBool()]; +} + +namespace n1 { + // The following should all come back as strings. + // They should be assignable to/from something of a type 'string'. + // They should not be assignable to either "Hello" or "World". + export let a = fun1("Hello", "World"); + export let b = fun1("Hello", "Hello"); + export let c = fun2("Hello", "World"); + export let d = fun2("Hello", "Hello"); + export let e = fun3("Hello", "Hello", "World", "Foo"); + + // Should be valid + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Passing these as arguments should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + +namespace n2 { + // The following (regardless of errors) should come back typed + // as "Hello" (or "Hello" | "Hello"). + export let a = fun1<"Hello">("Hello", "Hello"); + export let b = fun1<"Hello">("Hello", "World"); + export let c = fun2<"Hello", "Hello">("Hello", "Hello"); + export let d = fun2<"Hello", "Hello">("Hello", "World"); + export let e = fun3<"Hello">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Should be valid + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Assignment from the returned value should cause an error. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} + + +namespace n3 { + // The following (regardless of errors) should come back typed + // as "Hello" | "World" (or "World" | "Hello"). + export let a = fun2<"Hello", "World">("Hello", "World"); + export let b = fun2<"Hello", "World">("World", "Hello"); + export let c = fun2<"World", "Hello">("Hello", "Hello"); + export let d = fun2<"World", "Hello">("World", "World"); + export let e = fun3<"Hello" | "World">("Hello", "World"); + + // Assignment from the returned value should cause an error. + a = takeReturnString(a); + b = takeReturnString(b); + c = takeReturnString(c); + d = takeReturnString(d); + e = takeReturnString(e); + + // Passing these as arguments should cause an error. + a = takeReturnHello(a); + b = takeReturnHello(b); + c = takeReturnHello(c); + d = takeReturnHello(d); + e = takeReturnHello(e); + + // Both should be valid. + a = takeReturnHelloWorld(a); + b = takeReturnHelloWorld(b); + c = takeReturnHelloWorld(c); + d = takeReturnHelloWorld(d); + e = takeReturnHelloWorld(e); +} \ No newline at end of file diff --git a/tests/cases/fourslash/overloadOnConstCallSignature.ts b/tests/cases/fourslash/overloadOnConstCallSignature.ts index d729fd4da6f8d..101d7750e0424 100644 --- a/tests/cases/fourslash/overloadOnConstCallSignature.ts +++ b/tests/cases/fourslash/overloadOnConstCallSignature.ts @@ -11,7 +11,7 @@ goTo.marker('1'); verify.signatureHelpCountIs(4); -verify.currentSignatureHelpIs('foo(name: \'order\'): string'); +verify.currentSignatureHelpIs('foo(name: "order"): string'); edit.insert('"hi"'); goTo.marker('2'); diff --git a/tests/cases/fourslash/quickInfoDisplayPartsVarWithStringTypes01.ts b/tests/cases/fourslash/quickInfoDisplayPartsVarWithStringTypes01.ts new file mode 100644 index 0000000000000..b48c7cfa3de19 --- /dev/null +++ b/tests/cases/fourslash/quickInfoDisplayPartsVarWithStringTypes01.ts @@ -0,0 +1,41 @@ +/// + +////let /*1*/hello: "hello" | 'hello' = "hello"; +////let /*2*/world: 'world' = "world"; +////let /*3*/helloOrWorld: "hello" | 'world'; + +goTo.marker("1"); +verify.verifyQuickInfoDisplayParts("let", "", { start: test.markerByName('1').position, length: "hello".length }, [ + { text: "let", kind: "keyword" }, + { text: " ", kind: "space" }, + { text: "hello", kind: "localName" }, + { text: ":", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"hello"', kind: "stringLiteral" }, ], + /*documentation*/ []); + +goTo.marker("2"); +verify.verifyQuickInfoDisplayParts("let", "", { start: test.markerByName('2').position, length: "world".length }, [ + { text: "let", kind: "keyword" }, + { text: " ", kind: "space" }, + { text: "world", kind: "localName" }, + { text: ":", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"world"', kind: "stringLiteral" }, + ], + /*documentation*/[]); + +goTo.marker("3"); +verify.verifyQuickInfoDisplayParts("let", "", { start: test.markerByName('3').position, length: "helloOrWorld".length }, [ + { text: "let", kind: "keyword" }, + { text: " ", kind: "space" }, + { text: "helloOrWorld", kind: "localName" }, + { text: ":", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"hello"', kind: "stringLiteral" }, + { text: " ", kind: "space" }, + { text: "|", kind: "punctuation" }, + { text: " ", kind: "space" }, + { text: '"world"', kind: "stringLiteral" }, + ], + /*documentation*/[]); \ No newline at end of file diff --git a/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts b/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts index f2ee37aa675db..948c2fde99911 100644 --- a/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts +++ b/tests/cases/fourslash/quickInfoForOverloadOnConst1.ts @@ -20,22 +20,22 @@ ////c.x1(1, (x/*10*/x) => { return 1; } ); goTo.marker('1'); -verify.quickInfoIs("(method) I.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) I.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('2'); -verify.quickInfoIs("(method) C.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) C.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('3'); -verify.quickInfoIs("(parameter) callback: (x: 'hi') => number"); +verify.quickInfoIs("(parameter) callback: (x: \"hi\") => number"); goTo.marker('4'); -verify.quickInfoIs("(method) C.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) C.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('5'); verify.quickInfoIs('(parameter) callback: (x: string) => number'); goTo.marker('6'); verify.quickInfoIs('(parameter) callback: (x: string) => number'); goTo.marker('7'); -verify.quickInfoIs("(method) C.x1(a: number, callback: (x: 'hi') => number): any"); +verify.quickInfoIs("(method) C.x1(a: number, callback: (x: \"hi\") => number): any"); goTo.marker('8'); -verify.quickInfoIs("(parameter) xx: 'hi'"); +verify.quickInfoIs("(parameter) xx: \"hi\""); goTo.marker('9'); -verify.quickInfoIs("(parameter) xx: 'bye'"); +verify.quickInfoIs("(parameter) xx: \"bye\""); goTo.marker('10'); -verify.quickInfoIs("(parameter) xx: 'hi'"); \ No newline at end of file +verify.quickInfoIs("(parameter) xx: \"hi\""); \ No newline at end of file diff --git a/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts b/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts index 8edd0617d70ab..e312b71aebe58 100644 --- a/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts +++ b/tests/cases/fourslash/signatureHelpOnOverloadOnConst.ts @@ -18,9 +18,9 @@ verify.currentParameterSpanIs("z: string"); goTo.marker('2'); verify.signatureHelpCountIs(3); verify.currentParameterHelpArgumentNameIs("x"); -verify.currentParameterSpanIs("x: 'hi'"); +verify.currentParameterSpanIs("x: \"hi\""); goTo.marker('3'); verify.signatureHelpCountIs(3); verify.currentParameterHelpArgumentNameIs("y"); -verify.currentParameterSpanIs("y: 'bye'"); +verify.currentParameterSpanIs("y: \"bye\"");