Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String literal types #5185

Merged
merged 44 commits into from
Nov 10, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
311a0cf
Added tests.
DanielRosenwasser Sep 11, 2015
911e907
Added test for string literal types in type arguments.
DanielRosenwasser Oct 1, 2015
f04cc39
Accepted baselines.
DanielRosenwasser Oct 1, 2015
191be4f
Make string literals valid constituent types nodes in the parser.
DanielRosenwasser Oct 1, 2015
84786d8
Accepted baselines.
DanielRosenwasser Oct 1, 2015
dc0e368
Make string literals valid types in type lists.
DanielRosenwasser Oct 1, 2015
8891fba
Accepted baselines.
DanielRosenwasser Oct 1, 2015
82545ce
Added test for string types in tuples.
DanielRosenwasser Oct 1, 2015
ed927d8
Accepted baselines.
DanielRosenwasser Oct 1, 2015
a3e7ccb
Use normalized text for text on string literal types.
DanielRosenwasser Oct 2, 2015
20c2c4e
Amended fourslash tests to expect double quotes.
DanielRosenwasser Oct 2, 2015
87f2957
Accepted baselines.
DanielRosenwasser Oct 2, 2015
f721971
Capture compatible contextual types for unions containing string lite…
DanielRosenwasser Oct 2, 2015
7b4e94d
Accepted baselines.
DanielRosenwasser Oct 2, 2015
d8d72aa
Separated the concept of apparent types from contextual types for str…
DanielRosenwasser Oct 2, 2015
315b06d
Accepted baselines.
DanielRosenwasser Oct 2, 2015
4b736da
Fixed issue in test.
DanielRosenwasser Oct 2, 2015
fd5dec4
Accepted baselines.
DanielRosenwasser Oct 2, 2015
f7a6ac7
Added more tests.
DanielRosenwasser Oct 7, 2015
a440f06
Accepted baselines.
DanielRosenwasser Oct 8, 2015
d2e2a55
Added fourslash test.
DanielRosenwasser Oct 8, 2015
6e3343c
Merge branch 'master' into stringLiteralTypes
DanielRosenwasser Oct 8, 2015
74ac57d
Accepted post-merge baselines.
DanielRosenwasser Oct 8, 2015
61ece76
Return the string literal type itself instead of the union type.
DanielRosenwasser Oct 8, 2015
84b64c4
Accepted baselines.
DanielRosenwasser Oct 8, 2015
3788254
Semicolon.
DanielRosenwasser Oct 8, 2015
ebc47d5
Linting.
DanielRosenwasser Oct 8, 2015
725bda8
Merge branch 'master' into stringLiteralTypes
DanielRosenwasser Oct 15, 2015
ec0d49a
Always use a string literal type if contextually typed by any string …
DanielRosenwasser Oct 15, 2015
1dbd8d1
Accepted baselines.
DanielRosenwasser Oct 15, 2015
307d73e
Merge branch 'master' into stringLiteralTypes
DanielRosenwasser Oct 22, 2015
049d02f
Merge branch 'master' into stringLiteralTypes
DanielRosenwasser Oct 23, 2015
6618fd2
Added tests for operations that use assignable to/from.
DanielRosenwasser Oct 26, 2015
bf4880a
Merge branch 'master' into stringLiteralTypes
DanielRosenwasser Oct 26, 2015
5e23143
Accepted baselines.
DanielRosenwasser Oct 27, 2015
8dbfe1c
Added specific checks for comparing stringlike types.
DanielRosenwasser Nov 6, 2015
a1fcfaf
Accepted baselines.
DanielRosenwasser Nov 6, 2015
bb232f7
Merge remote-tracking branch 'origin/master' into stringLiteralTypes
DanielRosenwasser Nov 6, 2015
f939ff2
Fixed unreachable code in tests.
DanielRosenwasser Nov 6, 2015
d234b8d
Accepted baselines.
DanielRosenwasser Nov 6, 2015
c011ed4
Const.
DanielRosenwasser Nov 6, 2015
38090c6
Added tests for template strings with string literal types.
DanielRosenwasser Nov 9, 2015
d294524
Accepted baselines.
DanielRosenwasser Nov 9, 2015
ea4e21d
Fixed comments.
DanielRosenwasser Nov 9, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Accepted baselines.
  • Loading branch information
DanielRosenwasser committed Oct 1, 2015
commit 84786d82c14e3fae0f86d22e6fc635ecbac9dab8

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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); };
Expand Down
Original file line number Diff line number Diff line change
@@ -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, 620, 27))
>Math : Symbol(Math, Decl(lib.d.ts, 522, 1), Decl(lib.d.ts, 633, 11))
>sqrt : Symbol(Math.sqrt, Decl(lib.d.ts, 620, 27))
>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))

Original file line number Diff line number Diff line change
@@ -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 : Point
>x : number
>this.x : number
>this : Point
>x : number
>this.y * this.y : number
>this.y : number
>this : Point
>y : number
>this.y : number
>this : Point
>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

12 changes: 0 additions & 12 deletions tests/baselines/reference/stringLiteralType.errors.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/baselines/reference/stringLiteralType.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ function f(x: any) {
}

//// [stringLiteralType.js]
var x = 'hi';
var x;
function f(x) {
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/stringLiteralType.symbols
Original file line number Diff line number Diff line change
@@ -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))
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/stringLiteralType.types
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 5 additions & 27 deletions tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,27 @@
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,12): error TS4081: Exported type alias 'Kind' has or is using private name ''.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,13): error TS1110: Type expected.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(2,13): 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/stringLiteralTypesAsTags01.ts(2,19): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(9,10): error TS4033: Property 'kind' of exported interface has or is using private name ''.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(9,11): error TS1110: Type expected.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(14,10): error TS4033: Property 'kind' of exported interface has or is using private name ''.
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(14,11): error TS1110: Type expected.
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(25,5): error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'.
Property '"A"' is missing in type '{ kind: string; a: number; }'.
Types of property 'kind' are incompatible.
Type 'string' is not assignable to type '"A"'.


==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (13 errors) ====
==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (5 errors) ====

type Kind = "A" | "B"

!!! error TS4081: Exported type alias 'Kind' has or is using private name ''.
~~~
!!! error TS1110: Type expected.
~~~
!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
~~~
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

interface Entity {
kind: Kind;
}

interface A extends Entity {
kind: "A";

!!! error TS4033: Property 'kind' of exported interface has or is using private name ''.
~~~
!!! error TS1110: Type expected.
a: number;
}

interface B extends Entity {
kind: "B";

!!! error TS4033: Property 'kind' of exported interface has or is using private name ''.
~~~
!!! error TS1110: Type expected.
b: string;
}

Expand All @@ -66,7 +43,8 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(25,5):
let x: A = {
~
!!! error TS2322: Type '{ kind: string; a: number; }' is not assignable to type 'A'.
!!! error TS2322: Property '"A"' is missing in type '{ kind: string; a: number; }'.
!!! error TS2322: Types of property 'kind' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type '"A"'.
kind: "A",
a: 100,
}
Expand Down
20 changes: 19 additions & 1 deletion tests/baselines/reference/stringLiteralTypesAsTags01.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ else {
}

//// [stringLiteralTypesAsTags01.js]
"A" | "B";
function hasKind(entity, kind) {
return kind === is;
}
Expand All @@ -63,3 +62,22 @@ if (!hasKind(x, "B")) {
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;
Loading