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 — Type parameters as constraints by ahejlsberg · Pull Request #5949 · microsoft/TypeScript

Conversation

@ahejlsberg

@ahejlsberg Anders Hejlsberg (ahejlsberg) commented Dec 6, 2015

Copy link
Copy Markdown
Member

With this PR it becomes possible for a type parameter constraint to reference type parameters from the same type parameter list (this was previously an error). For example:

function assign<T extends U, U>(target: T, source: U): T {
    for (let id in source) {
        target[id] = source[id];
    }
    return target;
}

let x = { a: 1, b: 2, c: 3, d: 4 };
assign(x, { b: 10, d: 20 });
assign(x, { e: 0 });  // Error

A fancy term for this capability is F-Bounded Polymorphism.

Fixes #2304.

Sorry, something went wrong.

Uh oh!

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

stepancar, ulrichb, jonny-improbable, calvinlfer, olydis, and mgechev reacted with thumbs up emoji
All reactions
  • 👍 6 reactions
Anders Hejlsberg (ahejlsberg) added 6 commits December 1, 2015 15:51
@ahejlsberg
Allow type parameters in constraints
973ba4b
@ahejlsberg
Accepting new baselines
3750748
@ahejlsberg
Propagate 'this' type in constraint checking
94129a5
@ahejlsberg
Merge branch 'master' into typeParametersAsConstraints
99e6542
Conflicts:
	tests/baselines/reference/functionConstraintSatisfaction2.errors.txt
@ahejlsberg
Fix length check in typeArgumentsRelatedTo
c700bf9
@ahejlsberg
Accepting new baselines
2e56012
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Dec 6, 2015
@ahejlsberg
Fix linting errors
6fcceb4
DanielRosenwasser
DanielRosenwasser reviewed Dec 6, 2015
View reviewed changes
Comment thread src/compiler/checker.ts

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Dec 6, 2015

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.

Invert the condition to reduce nesting.

Sorry, something went wrong.

Uh oh!

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

All reactions

@ahejlsberg Anders Hejlsberg (ahejlsberg) Dec 6, 2015

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.

Not sure what you mean. There's a shared return statement at the bottom of the function and no else clause that would be reversed.

Sorry, something went wrong.

Uh oh!

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

All reactions

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Dec 9, 2015

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.

I meant to just repeat the return statement, but it's not really a big deal.

Sorry, something went wrong.

Uh oh!

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

All reactions
@ahejlsberg
Addressing CR feedback
30dbf9c
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Dec 8, 2015

Copy link
Copy Markdown
Member Author

Mohamed Hegazy (@mhegazy) Want to comment on this one before I merge it?

All reactions

Sorry, something went wrong.

Uh oh!

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

@ivogabe Ivo Gabe de Wolff (ivogabe) mentioned this pull request Dec 9, 2015
Suggestion: Improve generics inference with decorators #2686
Closed
@mhegazy

Mohamed Hegazy (mhegazy) commented Dec 9, 2015

Copy link
Copy Markdown
Contributor

👍

All reactions

Sorry, something went wrong.

Uh oh!

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

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Dec 10, 2015

Copy link
Copy Markdown
Member

Anders Hejlsberg (@ahejlsberg) I tried this out and upon grabbing quick info on a, I'm seeing an uninstantiated type:

interface Mapper<T> {
    map<U extends T, V extends U[]>(f: (item: T) => U): V;
}

var m: Mapper<string>;
var a = m.map((x: string) => x);

image

I expected a to have the type string[], so something isn't working out here.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Dec 10, 2015

Copy link
Copy Markdown
Member Author

Daniel Rosenwasser (@DanielRosenwasser) Good catch. The problem is that the constraints aren't getting properly instantiated. Will put up a fix shortly.

All reactions

Sorry, something went wrong.

Uh oh!

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

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Dec 10, 2015
inference strangeness - 3 #6037
Closed
@ahejlsberg
Include fresh type parameters in type parameter mapper function
9810ddf
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Dec 10, 2015

Copy link
Copy Markdown
Member

Hey Anders Hejlsberg (@ahejlsberg), Aleksey-Bykov wrote a useful example on #6037 which demonstrates the use of type parameters being able to extend each other. Can we add the following as a test case?

function fold<a, r>(values: a[], result: r, fold: (result: r, value: a) => r): r {
    for (let value of values) {
        result = fold(result, value);
    }
    return result;
}

function append<a, b extends a>(values: a[], value: b): a[] {
    values.push(value);
    return values;
}


fold(
    [1, 2, 3],
    [] as [string, string][],
    (result, value) => append(
        result,
        ["", ""]
    )
);
All reactions

Sorry, something went wrong.

Uh oh!

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

@zpdDG4gta8XKpMCd

ZpdDG4gta (zpdDG4gta8XKpMCd) commented Dec 10, 2015

Copy link
Copy Markdown

by the way

// test.ts
export function append<a, b extends a>(result: a[], value: b): a[] {
    result.push(value);
    return result;
}

compiles with

node ./built/local/tsc.js --module commonjs test.ts 

but fails with

node ./built/local/tsc.js --module commonjs --declaration test.ts 

saying

test.ts(1,37): error TS4016: Type parameter 'b' of exported function has or is using private name 'a'.
All reactions

Sorry, something went wrong.

Uh oh!

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

@zpdDG4gta8XKpMCd

ZpdDG4gta (zpdDG4gta8XKpMCd) commented Dec 10, 2015

Copy link
Copy Markdown

created a separate bug: #6040

All reactions

Sorry, something went wrong.

Uh oh!

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

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Dec 10, 2015

Copy link
Copy Markdown
Member

I noticed the same thing on my bug where the type parameter was not being instantiated. Can you enable // @declaration: true for all these test cases?

All reactions

Sorry, something went wrong.

Uh oh!

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

Anders Hejlsberg (ahejlsberg) added 3 commits December 10, 2015 13:54
@ahejlsberg
Adding test
3055445
@ahejlsberg
Fix declaration emitter scoping issue for type parameter constraints
5ab5338
@ahejlsberg
Adding tests
5cb2b6a
@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Dec 10, 2015

Copy link
Copy Markdown
Member Author

Latest commits fix the uninstantiated type issue as well as issue in #6040.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Dec 10, 2015
type parameters as constraints feature doesn't compile with --declaration argument #6040
Closed
@basarat Basarat Ali Syed (basarat) mentioned this pull request Dec 10, 2015
Type parameters as constraints basarat/typescript-book#47
Open
@basarat

Basarat Ali Syed (basarat) commented Dec 10, 2015

Copy link
Copy Markdown
Contributor

Awesome 🌹. I've done aweful things in the past : https://github.com/TypeScriptBuilder/tsb/blob/24c5bbaa3e29587ab0830194c374cd7fa8bb2462/src/app/state/simpleRedux.ts#L90-L103 :)

All reactions

Sorry, something went wrong.

Uh oh!

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

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Dec 11, 2015

Copy link
Copy Markdown
Member

Anders Hejlsberg (@ahejlsberg) I think it wouldn't be unreasonable to just get declaration emit for all of these tests.

All reactions

Sorry, something went wrong.

Uh oh!

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

Anders Hejlsberg (ahejlsberg) added a commit that referenced this pull request Dec 11, 2015
@ahejlsberg
Merge pull request #5949 from Microsoft/typeParametersAsConstraints
ff78477
Type parameters as constraints
@ahejlsberg
Anders Hejlsberg (ahejlsberg) merged commit ff78477 into master Dec 11, 2015
@ahejlsberg
Anders Hejlsberg (ahejlsberg) deleted the typeParametersAsConstraints branch December 11, 2015 00:54
@zpdDG4gta8XKpMCd

ZpdDG4gta (zpdDG4gta8XKpMCd) commented Dec 11, 2015

Copy link
Copy Markdown

Basarat Ali Syed (@basarat), a poorman's implementation of this feature could have been done by passing
a function that maps B to A thus enforcing the constraint, consider:

foo <a, b extends a>(foo: a, bar:b): void;

vs

foo <a, b>(foo: a, bar: b, bIsA: (val: b) => a): void;

All reactions

Sorry, something went wrong.

Uh oh!

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

@dead-claudia

Claudia Meadows (dead-claudia) commented Dec 17, 2015

Copy link
Copy Markdown

Question: does this patch support the following? I've come across this pattern way too frequently, and I've needed it for way too many things. In languages that support it, it's a very frequent idiom.

interface Type<T extends Type<T>> {}
All reactions

Sorry, something went wrong.

Uh oh!

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

@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Dec 17, 2015

Copy link
Copy Markdown
Member Author

@isiahmeadows Yes, that pattern (the "curiously recurring template pattern") is now supported. But also note that the polymorphic 'this' type is a more succinct way to accomplish the same thing.

All reactions

Sorry, something went wrong.

Uh oh!

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

@dead-claudia

Claudia Meadows (dead-claudia) commented Dec 17, 2015

Copy link
Copy Markdown

I also came across an unusual use case where I needed two F-bounded type
parameters, which the this type doesn't cover. Given this supports that
as well, I'm happy.

On Thu, Dec 17, 2015, 09:31 Anders Hejlsberg [email protected]
wrote:

@isiahmeadows https://github.com/isiahmeadows Yes, that pattern (the
"curiously recurring template pattern") is now supported. But also note
that the polymorphic 'this' type
#4910 is a more succinct
way to accomplish the same thing.

—
Reply to this email directly or view it on GitHub
#5949 (comment)
.

All reactions

Sorry, something went wrong.

Uh oh!

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

@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Dec 23, 2015
Question: Why can't type parameters be used inside type parameters? #6224
Closed
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Jan 4, 2016
Updating language specification #6345
Merged
@kitsonk Kitson Kelly (kitsonk) mentioned this pull request Jan 5, 2016
Freezing of the factory dojo/compose#16
Closed
@tomsdev Sunny64 (tomsdev) mentioned this pull request Jan 18, 2016
Partial Types (Optionalized Properties for Existing Types) #4889
Closed
@hayeah blah blah (hayeah) mentioned this pull request Feb 14, 2016
Type parameters as constraints dosen't work for methods #7079
Closed
@mhegazy Mohamed Hegazy (mhegazy) mentioned this pull request Feb 20, 2016
Support nested type parameters #214
Closed
This was referenced Feb 23, 2016
Terse API for Mixin dojo/compose#22
Closed
More accurate typing of Object.assign and React component setState() #6613
Closed
@spikefoo spikefoo mentioned this pull request May 1, 2016
Update Generics: It is now allowed for a type parameter to be constrained by another microsoft/TypeScript-Handbook#267
Closed
@coreh Marco Buono (coreh) mentioned this pull request Jun 3, 2016
Type inference not working for this particular use of Generics? #8952
Closed
@mishok13 Andrii Mishkovskyi (mishok13) mentioned this pull request Oct 5, 2017
Subclassing is useless for generated classes rthewhite/proto2immutablets#1
Open
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
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.

7 participants

@ahejlsberg @mhegazy @DanielRosenwasser @zpdDG4gta8XKpMCd @basarat @dead-claudia @msftclas
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.