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 — Supporting generic type inference over the other higher-order functions · Issue #9366 · microsoft/TypeScript

Supporting generic type inference over the other higher-order functions #9366

New issue
Copy link
New issue
Copy link
Closed
#30215
Closed
Supporting generic type inference over the other higher-order functions#9366
#30215
Copy link
Labels
Effort: DifficultGood luck.Good luck.FixedA PR has been merged for this issueA PR has been merged for this issueNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript

Description

@falsandtru
falsandtru (falsandtru)
opened on Jun 26, 2016
Issue body actions

Currently, TypeScript infers generic types when referring its type immediately. Its behavior is undesirable for using higher-order functions. The type inference of generics should delay until it will be called.

function flip<a, b, c>(f: (a: a, b: b) => c): (b: b, a: a) => c {
  return (b: b, a: a) => f(a, b);
}
function zip<T, U>(x: T, y: U): [T, U] {
  return [x, y];
}
var expected: <T, U>(y: U, x: T) => [T, U] = flip(zip);
var actual: (y: {}, x: {}) => [{}, {}] = flip(zip);
Reacted by Aluan Haddad, Adam Schroder, Sergey Shandar, Marin Marinov, François de Campredon, Dylan Schiemann, Dave Wasmer, Sean Newell, ZpdDG4gta, Zoey and 115 more
Reacted by Cristian Toma, Dylan Schiemann, ZpdDG4gta, Zoey, Emma, Erik Cupal, Dylan R. Johnston, Roy Tinker, Alexandre Galays, Daniel Popeski and 34 more

Activity

  1. Daniel Rosenwasser (DanielRosenwasser)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.
    Effort: DifficultGood luck.Good luck.
    on Jun 30, 2016
  2. fdecampredon commented on Dec 14, 2016

    @fdecampredon
    François de Campredon (fdecampredon)
    on Dec 14, 2016
    More actions

    this issue is really problematic for functional paradigm users :

    const map = <T, U>(transform: (t: T) => U) =>
        (arr: T[]) => arr.map(transform)
    
    const identity = <T>(t: T) => t;
    
    const identityStr = (t: string) => t;
    
    
    const arr: string[] = map(identityStr)(['a']);
    const arr1: string[] = map(identity)(['a']); // Type '{}[]' is not assignable to type 'string[]'.
    Reacted by Herrington Darkholme, Andrea Richiardi, Zoey, Daniel Yogel, Anders Hejlsberg, Will Frew, Thierry Régagnon, Erik Cupal, Roy Tinker, Daniel Popeski and 21 more
  3. DanielRosenwasser commented on Dec 15, 2016

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Dec 15, 2016
    Member
    More actions

    (welcome back François de Campredon (@fdecampredon)!)

    Reacted by Mohamed Hegazy, kiara, Erik Cupal, Daniel Popeski and Tycho Tatitscheff
    Reacted by Mohamed Hegazy, Ryan Cavanaugh, Erik Cupal, Daniel Popeski and Acid Chicken
  4. fdecampredon commented on Dec 15, 2016

    @fdecampredon
    François de Campredon (fdecampredon)
    on Dec 15, 2016
    More actions

    (thanks ! :) )

  5. baio commented on Jan 19, 2017

    @baio
    baio (baio)
    on Jan 19, 2017
    More actions

    Yep, there is to many <any> in my Ramda code.

  6. Igorbek commented on Feb 21, 2017

    @Igorbek
    Igor Oleinikov (Igorbek)
    on Feb 21, 2017
    Contributor
    More actions

    #10247, #9949 for the reference. And see Marin Marinov (@gcnew)'s comment

  7. masaeedu commented on Jul 31, 2017

    @masaeedu
    Asad Saeeduddin (masaeedu)
    on Jul 31, 2017
    Contributor
    More actions

    Relevant paper: "Practical type inference for higher-rank types". Specifically the section on subsumption of parametrically polymorphic signatures.

    Ultimately this comes down to better unification, which is what Marin Marinov (@gcnew) has done some work on if you follow along in #9949. For the flip example above, the rank 1 function type <T, U>(x: T, y: U) => [T, U] can be substituted for the rank 0 function type (a: a) => (b: b) => c to obtain f: (x: a, y: b) => [a, b], because the former is more polymorphic than the latter. From there we need TypeScript's usual approach to unification to kick in and infer c = [a, b]. The tricky part is coming up with a general algorithm to do all of this.

    Reacted by Roy Tinker, Daniel Popeski, Cameron Martin, Nate Abele, James Conkling, Abraham White and FTS
  8. pelotom commented on Nov 6, 2017

    @pelotom
    Tom Crockett (pelotom)
    on Nov 6, 2017
    More actions

    Another example:

    const compose = <A, B, C>(g: (b: B) => C, f: (a: A) => B) => (a: A) => g(f(a))
    const identity = <T>(x: T) => x
    const lessThanTen = (x: number) => x < 10
    const composed = compose(lessThanTen, identity)
    //                       ^^^^^^^^^^^
    // Argument of type '(x: number) => boolean' is not assignable to parameter of type '(b: {}) => boolean'.
    //   Types of parameters 'x' and 'b' are incompatible.
    //     Type '{}' is not assignable to type 'number'.

    Can be fixed with explicit type parameters

    const composed = compose<number, number, boolean>(lessThanTen, identity)

    but would be really nice if this could just unify!

  9. Birowsky commented on Dec 22, 2017

    @Birowsky
    Daniel Popeski (Birowsky)
    on Dec 22, 2017
    More actions

    I would love to know if there is some movement here. Here's another usecase: https://stackoverflow.com/q/47934804/592641

    Reacted by Igor Soloydenko
  10. cameron-martin commented on Mar 5, 2018

    @cameron-martin
    Cameron Martin (cameron-martin)
    on Mar 5, 2018
    More actions

    I've ran into this issue when using lodash's memoize function when the function being memoised is generic.

  11. steve8708 commented on May 3, 2018

    @steve8708
    Steve Sewell (steve8708)
    on May 3, 2018
    More actions

    This would also be really valuable for mixins on react components with proptypes

    export class MyComponent extends MyMixin(React.Component<MyComponentProps>) {}

    I love the new mixins feature since 2.2 and I was really excited about refactoring my code to create a variety of mixins for my components until I realized as of right now it won't quite work as I'd hoped without a lot of manual type declaring 😭

  12. kpdonn commented on Jun 2, 2018

    @kpdonn
    Kevin Donnelly (kpdonn)
    on Jun 2, 2018
    Contributor
    More actions

    Heads up I put up my attempt at implementing this at #24626. No idea if it'll go anywhere but it works for a lot of the cases I've tried so feel free to try it out if you're interested.

    Reacted by Milos Nedeljkovic, Daniel Popeski, Aaron Friel, kiara, deepalma, Bastian Sieker, Oliver Joseph Ash, Marin Marinov, falsandtru, Alex Fox Gill and 5 more
    Reacted by falsandtru, Josh Burgess and Nate Abele
  13. nateabele commented on Jun 2, 2018

    @nateabele
    Nate Abele (nateabele)
    on Jun 2, 2018
    More actions

    Kevin Donnelly (@kpdonn) 😭 It's so beautiful.

    Reacted by Kevin Donnelly, kiara, deepalma, Joshua Weiss, Josh Burgess and James Conkling
  14. sibelius commented on Jul 20, 2018

    @sibelius
    Sibelius Seraphini (sibelius)
    on Jul 20, 2018
    More actions

    Any progress on this?

    Reacted by Jiali Chen, Axel Hernández Ferrera, Martin Kock, Abraham White, Evan Carroll, MattiasMartens, Nate Abele, Ricky Rattlesnake, Giacomo Tagliabue and Joni Katajamäki
  15. ahejlsberg commented on Mar 4, 2019

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on Mar 4, 2019
    Member
    More actions

    Higher order function type inference now implemented in #30215.

    Reacted by Stéphane Le Dorze, James Conkling, Nate Abele, John Reilly, kiara, Sergey, Lucas Basquerotto, Derrick Beining, Brett Larson, SlurpTheo and 18 more
    Reacted by Nate Abele, John Reilly, kiara, Derrick Beining, malinin, Igor Soloydenko, Joshua Weiss, kristianhasselknippe, Anders Bech Mellson, Jules Sam. Randolph and 5 more
    Reacted by John Reilly, kiara, Alex Ketch, Derrick Beining, Igor Soloydenko, Joshua Weiss, kristianhasselknippe, Jules Sam. Randolph, Changsheng Zhu and Janek Eilts UG (haftungsbeschränkt)
    Reacted by Stéphane Le Dorze, Nate Abele, John Reilly, kiara, btoo, Derrick Beining, malinin, Igor Soloydenko, Joshua Weiss, kristianhasselknippe and 4 more
  16. Anders Hejlsberg (ahejlsberg)
    closed this as completedin #30215on Mar 8, 2019
  17. Anders Hejlsberg (ahejlsberg)
    added
    FixedA PR has been merged for this issueA PR has been merged for this issue
    on Mar 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Effort: DifficultGood luck.Good luck.FixedA PR has been merged for this issueA PR has been merged for this issueNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.