GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||
GeezerVison is showing github.com — Adding support for tuple types (e.g. [number, string]) by ahejlsberg · Pull Request #428 · microsoft/TypeScript Conversation
Member
This commit adds support for typle types. A tuple type is written as a comma separated sequence of types enclosed in square brackets: [T0, T1, ... , Tn] A tuple type corresponds to an object type that extends { 0: T0; 1: T1; ... n: Tn; } 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: var t: [number, string] = [1, "hello"]; t = []; // Error t = [1]; // Error t = [2, "test"]; // Ok t = ["test", 2]; // Error t = [2, "test", true]; // Ok 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: var x: [number, string] = [1, "hello"]; var x0 = x[0]; // Type number var x1 = x[1]; // Type string var x2 = x[2]; // Type {} A tuple type is assignable to a compatible array type. For example: var a1: number[]; var a2: {}[]; var t1: [number, string]; var t2: [number, number]; a1 = t1; // Error a1 = t2; // Ok a2 = t1; // Ok a2 = t2; // Ok Type inference works as expected for tuple types: function tuple2<T0, T1>(item0: T0, item1: T1): [T0, T1] { return [item0, item1]; } var t = tuple2("test", true); var t0 = t[0]; // string var t1 = t[1]; // boolean Next step is to support EcmaScript 6 style destructuring of objects, arrays, and tuples.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add newline after the &&
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good ol' η-reduction.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. someone ate my tea
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Someone eta my tea
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Closed
Conflicts: tests/baselines/reference/typeName1.errors.txt …bsence of numerically named properties and doesn't directly test for tuple types.
Member
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Anders Hejlsberg (ahejlsberg)
added a commit
that referenced
this pull request
Adding support for tuple types (e.g. [number, string])
Merged
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
ReviewersNo reviewsUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
None yet
ProjectsMilestoneNo milestoneDevelopmentSuccessfully merging this pull request may close these issues. Uh oh!
There was an error while loading. Please reload this page. Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later. | ||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |