-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Adding support for tuple types (e.g. [number, string]) #428
Conversation
// class C { | ||
// constructor(public x?) { } | ||
// } | ||
// | ||
// x is an optional parameter, but it is a required property. | ||
return (propertySymbol.valueDeclaration.flags & NodeFlags.QuestionMark) && propertySymbol.valueDeclaration.kind !== SyntaxKind.Parameter; | ||
return propertySymbol.valueDeclaration && propertySymbol.valueDeclaration.flags & NodeFlags.QuestionMark && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add newline after the &&
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup.
This looks good. but i'm not sure now is the right time to be working on new features when we haven't even reached parity yet with the existing compiler. We need to get things like the TypeWriter and what not back online before we make changes that might introduce regressions that we don't have the infrastructure in place to catch. |
a1 = a2; // Error | ||
a1 = a3; // Error | ||
a3 = a1; | ||
a3 = a2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be worth breaking this up into a few different files by concept (ex tupleTypeInference, etc).
We also need a lot more coverage here. We should have tests for things like
var x = 0;
var y = 3;
var tt = tuple2(1, "a");
var r1 = tt[x];
var r2 = tt[y];
Tuples crossing module boundaries, usages in class hierarchies with array types, assignability with nested tuples and nested arrays, etc.
♨️ 😎 |
Looks good, although please validate this with typeWriter (not online yet) before merging into master |
@@ -2037,7 +2070,7 @@ module ts { | |||
if (type.flags & (TypeFlags.Class | TypeFlags.Interface) && type.flags & TypeFlags.Reference) { | |||
var typeParameters = (<InterfaceType>type).typeParameters; | |||
if (node.typeArguments && node.typeArguments.length === typeParameters.length) { | |||
type = createTypeReference(<GenericType>type, map(node.typeArguments, t => getTypeFromTypeNode(t))); | |||
type = createTypeReference(<GenericType>type, map(node.typeArguments, getTypeFromTypeNode)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good ol' η-reduction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
someone ate my tea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Someone eta my tea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Conflicts: tests/baselines/reference/typeName1.errors.txt
…bsence of numerically named properties and doesn't directly test for tuple types.
I have revised the contextual typing logic for array literals based on the discussions we had at the design meeting. Specifically: In an array literal contextually typed by a type T, the contextual type of an element expression at index N is the type of the property with the numeric name N in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one exists. When at least one element expression is contextually typed by a numerically named property, the resulting type of the array literal expression is a tuple type. Otherwise the resulting type is an array type. With these rules there's nothing special about tuple types when it comes to contextual typing, and manually defined tuple-like type works just as well. For example: interface Tuple2<T0, T1> {
0: T0;
1: T1;
}
var t: Tuple2<number, string> = [1, "hello"];
var x = t[0]; // number
var y = t[1]; // string |
@@ -149,6 +149,7 @@ module ts { | |||
TypeQuery, | |||
TypeLiteral, | |||
ArrayType, | |||
TupleType, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After you merge the changes from typeWriter (now in master), be sure to modify SyntaxKind.LastTypeNode accordingly.
Adding support for tuple types (e.g. [number, string])
Was reading and wondered why ~ const a2: [number, string] = [1, "2"];
const a3: string | number = a2.shift(); // ["2"] left in array
const a4: string = a2[0]; // ERROR: Type 'number' is not assignable to type 'string'. Just got this in the (2.0) playground. |
@SlurpTheo because Tuple is a subtype of Array. |
This commit adds support for typle types. A tuple type is written as a comma separated sequence of types enclosed in square brackets:
A tuple type corresponds to an object type that extends
Array<T>
, whereT
is the best common type of the tuple element types, with a sequence of numerically named members:When an array literal is contextually typed by a tuple type, each of the array literal expressions are contextually typed by the corresponding tuple element type, and the result is a tuple type:
When a value of a tuple type is indexed with a numeric constant, the resulting type is that of the corresponding type element. For example:
A tuple type is assignable to a compatible array type. For example:
Type inference works as expected for tuple types:
Next step is to support EcmaScript 6 style destructuring of objects, arrays, and tuples.