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

Make tuples have known length #17765

Merged
merged 17 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace ts {
const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks");
const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
const strictTuples = getStrictOptionValue(compilerOptions, "strictTuples");
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");

Expand Down Expand Up @@ -7302,6 +7303,12 @@ namespace ts {
property.type = typeParameter;
properties.push(property);
}
if (strictTuples) {
const lengthSymbol = createSymbol(SymbolFlags.Property, "length" as __String);
lengthSymbol.type = getLiteralType(arity);
lengthSymbol.checkFlags = CheckFlags.Readonly;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how adding Readonly helps if the type is literally 2, because only 2 can be assigned to it. If the intent is future-proofing in case Readonly will someday propagate through assignments, I think that it's as likely to harm as it is to help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fair enough. I had no special intentions in adding it. Feel free to adjust as you see fit! :)

properties.push(lengthSymbol);
}
const type = <GenericType & InterfaceTypeWithDeclaredMembers>createObjectType(ObjectFlags.Tuple | ObjectFlags.Reference);
type.typeParameters = typeParameters;
type.outerTypeParameters = undefined;
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ namespace ts {
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_checking_of_function_types
},
{
name: "strictTuples",
type: "boolean",
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Enable_strict_tuple_checks
},
{
name: "noImplicitThis",
type: "boolean",
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ namespace ts {
return moduleResolution;
}

export type StrictOptionName = "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "alwaysStrict";
export type StrictOptionName = "noImplicitAny" | "noImplicitThis" | "strictNullChecks" | "strictFunctionTypes" | "strictTuples" | "alwaysStrict";

export function getStrictOptionValue(compilerOptions: CompilerOptions, flag: StrictOptionName): boolean {
return compilerOptions[flag] === undefined ? compilerOptions.strict : compilerOptions[flag];
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3326,6 +3326,10 @@
"category": "Message",
"code": 6186
},
"Enable strict tuple checks.": {
"category": "Message",
"code": 6187
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3785,6 +3785,7 @@ namespace ts {
strict?: boolean;
strictFunctionTypes?: boolean; // Always combine with strict property
strictNullChecks?: boolean; // Always combine with strict property
strictTuples?: boolean;
/* @internal */ stripInternal?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
Expand Down
6 changes: 6 additions & 0 deletions src/harness/unittests/configurationExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace ts {
strictNullChecks: false
}
},
"/dev/tsconfig.strictTuples.json": {
extends: "./tsconfig",
compilerOptions: {
strictTuples: false
}
},
"/dev/configs/base.json": {
compilerOptions: {
allowJs: true,
Expand Down
4 changes: 4 additions & 0 deletions src/harness/unittests/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ var x = 0;`, {
options: { compilerOptions: { strictNullChecks: true }, fileName: "input.js", reportDiagnostics: true }
});

transpilesCorrectly("Supports setting 'strictTuples'", "x;", {
options: { compilerOptions: { strictTuples: true }, fileName: "input.js", reportDiagnostics: true }
});

transpilesCorrectly("Supports setting 'stripInternal'", "x;", {
options: { compilerOptions: { stripInternal: true }, fileName: "input.js", reportDiagnostics: true }
});
Expand Down
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,7 @@ namespace ts.server.protocol {
sourceRoot?: string;
strict?: boolean;
strictNullChecks?: boolean;
strictTuples?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget | ts.ScriptTarget;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,7 @@ declare namespace ts {
strict?: boolean;
strictFunctionTypes?: boolean;
strictNullChecks?: boolean;
strictTuples?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget;
Expand Down Expand Up @@ -6846,6 +6847,7 @@ declare namespace ts.server.protocol {
sourceRoot?: string;
strict?: boolean;
strictNullChecks?: boolean;
strictTuples?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget | ts.ScriptTarget;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,7 @@ declare namespace ts {
strict?: boolean;
strictFunctionTypes?: boolean;
strictNullChecks?: boolean;
strictTuples?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/genericTypeAliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type Strange<T> = string; // Type parameter not used
var s: Strange<number>;
s = "hello";

interface Tuple<A, B> {
interface AB<A, B> {
a: A;
b: B;
}

type Pair<T> = Tuple<T, T>;
type Pair<T> = AB<T, T>;

interface TaggedPair<T> extends Pair<T> {
tag: string;
Expand Down
32 changes: 16 additions & 16 deletions tests/baselines/reference/genericTypeAliases.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,29 @@ var s: Strange<number>;
s = "hello";
>s : Symbol(s, Decl(genericTypeAliases.ts, 38, 3))

interface Tuple<A, B> {
>Tuple : Symbol(Tuple, Decl(genericTypeAliases.ts, 39, 12))
>A : Symbol(A, Decl(genericTypeAliases.ts, 41, 16))
>B : Symbol(B, Decl(genericTypeAliases.ts, 41, 18))
interface AB<A, B> {
>AB : Symbol(AB, Decl(genericTypeAliases.ts, 39, 12))
>A : Symbol(A, Decl(genericTypeAliases.ts, 41, 13))
>B : Symbol(B, Decl(genericTypeAliases.ts, 41, 15))

a: A;
>a : Symbol(Tuple.a, Decl(genericTypeAliases.ts, 41, 23))
>A : Symbol(A, Decl(genericTypeAliases.ts, 41, 16))
>a : Symbol(AB.a, Decl(genericTypeAliases.ts, 41, 20))
>A : Symbol(A, Decl(genericTypeAliases.ts, 41, 13))

b: B;
>b : Symbol(Tuple.b, Decl(genericTypeAliases.ts, 42, 9))
>B : Symbol(B, Decl(genericTypeAliases.ts, 41, 18))
>b : Symbol(AB.b, Decl(genericTypeAliases.ts, 42, 9))
>B : Symbol(B, Decl(genericTypeAliases.ts, 41, 15))
}

type Pair<T> = Tuple<T, T>;
type Pair<T> = AB<T, T>;
>Pair : Symbol(Pair, Decl(genericTypeAliases.ts, 44, 1))
>T : Symbol(T, Decl(genericTypeAliases.ts, 46, 10))
>Tuple : Symbol(Tuple, Decl(genericTypeAliases.ts, 39, 12))
>AB : Symbol(AB, Decl(genericTypeAliases.ts, 39, 12))
>T : Symbol(T, Decl(genericTypeAliases.ts, 46, 10))
>T : Symbol(T, Decl(genericTypeAliases.ts, 46, 10))

interface TaggedPair<T> extends Pair<T> {
>TaggedPair : Symbol(TaggedPair, Decl(genericTypeAliases.ts, 46, 27))
>TaggedPair : Symbol(TaggedPair, Decl(genericTypeAliases.ts, 46, 24))
>T : Symbol(T, Decl(genericTypeAliases.ts, 48, 21))
>Pair : Symbol(Pair, Decl(genericTypeAliases.ts, 44, 1))
>T : Symbol(T, Decl(genericTypeAliases.ts, 48, 21))
Expand All @@ -157,17 +157,17 @@ interface TaggedPair<T> extends Pair<T> {

var p: TaggedPair<number>;
>p : Symbol(p, Decl(genericTypeAliases.ts, 52, 3))
>TaggedPair : Symbol(TaggedPair, Decl(genericTypeAliases.ts, 46, 27))
>TaggedPair : Symbol(TaggedPair, Decl(genericTypeAliases.ts, 46, 24))

p.a = 1;
>p.a : Symbol(Tuple.a, Decl(genericTypeAliases.ts, 41, 23))
>p.a : Symbol(AB.a, Decl(genericTypeAliases.ts, 41, 20))
>p : Symbol(p, Decl(genericTypeAliases.ts, 52, 3))
>a : Symbol(Tuple.a, Decl(genericTypeAliases.ts, 41, 23))
>a : Symbol(AB.a, Decl(genericTypeAliases.ts, 41, 20))

p.b = 2;
>p.b : Symbol(Tuple.b, Decl(genericTypeAliases.ts, 42, 9))
>p.b : Symbol(AB.b, Decl(genericTypeAliases.ts, 42, 9))
>p : Symbol(p, Decl(genericTypeAliases.ts, 52, 3))
>b : Symbol(Tuple.b, Decl(genericTypeAliases.ts, 42, 9))
>b : Symbol(AB.b, Decl(genericTypeAliases.ts, 42, 9))

p.tag = "test";
>p.tag : Symbol(TaggedPair.tag, Decl(genericTypeAliases.ts, 48, 41))
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/genericTypeAliases.types
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ s = "hello";
>s : string
>"hello" : "hello"

interface Tuple<A, B> {
>Tuple : Tuple<A, B>
interface AB<A, B> {
>AB : AB<A, B>
>A : A
>B : B

Expand All @@ -172,17 +172,17 @@ interface Tuple<A, B> {
>B : B
}

type Pair<T> = Tuple<T, T>;
>Pair : Tuple<T, T>
type Pair<T> = AB<T, T>;
>Pair : AB<T, T>
>T : T
>Tuple : Tuple<A, B>
>AB : AB<A, B>
>T : T
>T : T

interface TaggedPair<T> extends Pair<T> {
>TaggedPair : TaggedPair<T>
>T : T
>Pair : Tuple<T, T>
>Pair : AB<T, T>
>T : T

tag: string;
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/nominalSubtypeCheckOfTypeParameter.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//// [nominalSubtypeCheckOfTypeParameter.ts]
interface Tuple<T, S> {
interface BinaryTuple<T, S> {
first: T
second: S
second: S
}

interface Sequence<T> {
hasNext(): boolean
pop(): T
zip<S>(seq: Sequence<S>): Sequence<Tuple<T, S>>
pop(): T
zip<S>(seq: Sequence<S>): Sequence<BinaryTuple<T, S>>
}

// error, despite the fact that the code explicitly says List<T> extends Sequence<T>, the current rules for infinitely expanding type references
// perform nominal subtyping checks that allow variance for type arguments, but not nominal subtyping for the generic type itself
interface List<T> extends Sequence<T> {
getLength(): number
zip<S>(seq: Sequence<S>): List<Tuple<T, S>>
zip<S>(seq: Sequence<S>): List<BinaryTuple<T, S>>
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
=== tests/cases/conformance/types/typeRelationships/recursiveTypes/nominalSubtypeCheckOfTypeParameter.ts ===
interface Tuple<T, S> {
>Tuple : Symbol(Tuple, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 0))
>T : Symbol(T, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 16))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 18))
interface BinaryTuple<T, S> {
>BinaryTuple : Symbol(BinaryTuple, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 0))
>T : Symbol(T, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 22))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 24))

first: T
>first : Symbol(Tuple.first, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 23))
>T : Symbol(T, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 16))
>first : Symbol(BinaryTuple.first, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 29))
>T : Symbol(T, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 22))

second: S
>second : Symbol(Tuple.second, Decl(nominalSubtypeCheckOfTypeParameter.ts, 1, 12))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 18))
second: S
>second : Symbol(BinaryTuple.second, Decl(nominalSubtypeCheckOfTypeParameter.ts, 1, 12))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 24))
}

interface Sequence<T> {
Expand All @@ -20,20 +20,20 @@ interface Sequence<T> {
hasNext(): boolean
>hasNext : Symbol(Sequence.hasNext, Decl(nominalSubtypeCheckOfTypeParameter.ts, 5, 23))

pop(): T
pop(): T
>pop : Symbol(Sequence.pop, Decl(nominalSubtypeCheckOfTypeParameter.ts, 6, 22))
>T : Symbol(T, Decl(nominalSubtypeCheckOfTypeParameter.ts, 5, 19))

zip<S>(seq: Sequence<S>): Sequence<Tuple<T, S>>
>zip : Symbol(Sequence.zip, Decl(nominalSubtypeCheckOfTypeParameter.ts, 7, 14))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 10))
>seq : Symbol(seq, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 13))
zip<S>(seq: Sequence<S>): Sequence<BinaryTuple<T, S>>
>zip : Symbol(Sequence.zip, Decl(nominalSubtypeCheckOfTypeParameter.ts, 7, 12))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 8))
>seq : Symbol(seq, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 11))
>Sequence : Symbol(Sequence, Decl(nominalSubtypeCheckOfTypeParameter.ts, 3, 1))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 10))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 8))
>Sequence : Symbol(Sequence, Decl(nominalSubtypeCheckOfTypeParameter.ts, 3, 1))
>Tuple : Symbol(Tuple, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 0))
>BinaryTuple : Symbol(BinaryTuple, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 0))
>T : Symbol(T, Decl(nominalSubtypeCheckOfTypeParameter.ts, 5, 19))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 10))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 8, 8))
}

// error, despite the fact that the code explicitly says List<T> extends Sequence<T>, the current rules for infinitely expanding type references
Expand All @@ -47,15 +47,15 @@ interface List<T> extends Sequence<T> {
getLength(): number
>getLength : Symbol(List.getLength, Decl(nominalSubtypeCheckOfTypeParameter.ts, 13, 39))

zip<S>(seq: Sequence<S>): List<Tuple<T, S>>
zip<S>(seq: Sequence<S>): List<BinaryTuple<T, S>>
>zip : Symbol(List.zip, Decl(nominalSubtypeCheckOfTypeParameter.ts, 14, 23))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 10))
>seq : Symbol(seq, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 13))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 8))
>seq : Symbol(seq, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 11))
>Sequence : Symbol(Sequence, Decl(nominalSubtypeCheckOfTypeParameter.ts, 3, 1))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 10))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 8))
>List : Symbol(List, Decl(nominalSubtypeCheckOfTypeParameter.ts, 9, 1))
>Tuple : Symbol(Tuple, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 0))
>BinaryTuple : Symbol(BinaryTuple, Decl(nominalSubtypeCheckOfTypeParameter.ts, 0, 0))
>T : Symbol(T, Decl(nominalSubtypeCheckOfTypeParameter.ts, 13, 15))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 10))
>S : Symbol(S, Decl(nominalSubtypeCheckOfTypeParameter.ts, 15, 8))
}

20 changes: 10 additions & 10 deletions tests/baselines/reference/nominalSubtypeCheckOfTypeParameter.types
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
=== tests/cases/conformance/types/typeRelationships/recursiveTypes/nominalSubtypeCheckOfTypeParameter.ts ===
interface Tuple<T, S> {
>Tuple : Tuple<T, S>
interface BinaryTuple<T, S> {
>BinaryTuple : BinaryTuple<T, S>
>T : T
>S : S

first: T
>first : T
>T : T

second: S
second: S
>second : S
>S : S
}
Expand All @@ -20,18 +20,18 @@ interface Sequence<T> {
hasNext(): boolean
>hasNext : () => boolean

pop(): T
pop(): T
>pop : () => T
>T : T

zip<S>(seq: Sequence<S>): Sequence<Tuple<T, S>>
>zip : <S>(seq: Sequence<S>) => Sequence<Tuple<T, S>>
zip<S>(seq: Sequence<S>): Sequence<BinaryTuple<T, S>>
>zip : <S>(seq: Sequence<S>) => Sequence<BinaryTuple<T, S>>
>S : S
>seq : Sequence<S>
>Sequence : Sequence<T>
>S : S
>Sequence : Sequence<T>
>Tuple : Tuple<T, S>
>BinaryTuple : BinaryTuple<T, S>
>T : T
>S : S
}
Expand All @@ -47,14 +47,14 @@ interface List<T> extends Sequence<T> {
getLength(): number
>getLength : () => number

zip<S>(seq: Sequence<S>): List<Tuple<T, S>>
>zip : <S>(seq: Sequence<S>) => List<Tuple<T, S>>
zip<S>(seq: Sequence<S>): List<BinaryTuple<T, S>>
>zip : <S>(seq: Sequence<S>) => List<BinaryTuple<T, S>>
>S : S
>seq : Sequence<S>
>Sequence : Sequence<T>
>S : S
>List : List<T>
>Tuple : Tuple<T, S>
>BinaryTuple : BinaryTuple<T, S>
>T : T
>S : S
}
Expand Down
Loading