GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Adding support for tuple types (e.g. [number, string]) by ahejlsberg · Pull Request #428 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Aug 11, 2014

Copy link
Copy Markdown
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 Array<T>, where T is the best common type of the tuple element types, with a sequence of numerically named members:

{
    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
@ahejlsberg
Adding support for tuple types (e.g. [number, string])
5b25524
CyrusNajmabadi
CyrusNajmabadi reviewed Aug 11, 2014
View reviewed changes
Comment thread src/compiler/checker.ts Outdated

@CyrusNajmabadi Cyrus Najmabadi (CyrusNajmabadi) Aug 11, 2014

Copy link
Copy Markdown
Contributor

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 &&

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Aug 11, 2014

Copy link
Copy Markdown
Member Author

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@CyrusNajmabadi

Cyrus Najmabadi (CyrusNajmabadi) commented Aug 11, 2014

Copy link
Copy Markdown
Contributor

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

danquirk
danquirk reviewed Aug 11, 2014
View reviewed changes
Comment thread tests/cases/compiler/tupleTypes.ts

@danquirk Dan Quirk (danquirk) Aug 11, 2014

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahejlsberg
Addressing CR feedback, adding baselines.
3b1dbad
@basarat

Basarat Ali Syed (basarat) commented Aug 12, 2014

Copy link
Copy Markdown
Contributor

♨️ 😎

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JsonFreeman

Jason Freeman (JsonFreeman) commented Aug 12, 2014

Copy link
Copy Markdown
Contributor

Looks good, although please validate this with typeWriter (not online yet) before merging into master

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

DanielRosenwasser
DanielRosenwasser reviewed Aug 13, 2014
View reviewed changes
Comment thread src/compiler/checker.ts

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Aug 13, 2014

Copy link
Copy Markdown
Member

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@basarat Basarat Ali Syed (basarat) Aug 13, 2014

Copy link
Copy Markdown
Contributor

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JsonFreeman Jason Freeman (JsonFreeman) Aug 13, 2014

Copy link
Copy Markdown
Contributor

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Aug 13, 2014

Copy link
Copy Markdown
Member

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.

👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Anders Hejlsberg (ahejlsberg) added 3 commits August 13, 2014 07:15
@ahejlsberg
Addressing CR feedback.
ef52312
@ahejlsberg
Adding error message for empty tuple types.
92b3677
@ahejlsberg
Accepting new baselines.
f0b33b3
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request Aug 15, 2014
Destructuring (ES6) #240
Closed
Anders Hejlsberg (ahejlsberg) added 3 commits August 16, 2014 11:11
@ahejlsberg
Merge branch 'master' into tupleTypes
0cf503f
Conflicts:
	tests/baselines/reference/typeName1.errors.txt
@ahejlsberg
Accepting new baselines after merge.
c0e802d
@ahejlsberg
Contextual typing of array literals is now based on the presence or a…
63b83e7
…bsence

of numerically named properties and doesn't directly test for tuple types.
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Aug 17, 2014

Copy link
Copy Markdown
Member Author

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

JsonFreeman
JsonFreeman reviewed Aug 19, 2014
View reviewed changes
Comment thread src/compiler/types.ts

@JsonFreeman Jason Freeman (JsonFreeman) Aug 19, 2014

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Anders Hejlsberg (ahejlsberg) added 2 commits September 5, 2014 13:52
@ahejlsberg
Merge branch 'master' into tupleTypes
be7e0a7
@ahejlsberg
Merge branch 'master' into tupleTypes
b4cddc3
Anders Hejlsberg (ahejlsberg) added a commit that referenced this pull request Sep 15, 2014
@ahejlsberg
Merge pull request #428 from Microsoft/tupleTypes
11b9118
Adding support for tuple types (e.g. [number, string])
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit 11b9118 into master Sep 15, 2014
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the tupleTypes branch September 15, 2014 19:58
@yuit Yui (yuit) mentioned this pull request Oct 4, 2014
Tuple conformance #822
Merged
@saschanaz Kagami Sascha Rosylight (saschanaz) mentioned this pull request Nov 1, 2014
Typed ...rest parameters with generics #1024
Closed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@ahejlsberg @CyrusNajmabadi @basarat @JsonFreeman @SlurpTheo @aluanhaddad @DanielRosenwasser @danquirk
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.