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 — Contextual typing does not flow backward through function return types to the function arguments · Issue #5256 · microsoft/TypeScript

Contextual typing does not flow backward through function return types to the function arguments #5256

New issue
Copy link
New issue
Copy link
Closed
Closed
Contextual typing does not flow backward through function return types to the function arguments#5256
Copy link
Labels
DuplicateAn existing issue was already createdAn existing issue was already created

Description

@drarmstr
Douglas Armstrong (drarmstr)
opened on Oct 14, 2015
Issue body actions

Consider the following case:

class Entry<T> {
    constructor(callback: (v: T) => number) { }
}
interface Widget {
    list: Entry<{ foo: number }>[];
}

var widget: Widget = {
    list: [
        new Entry((v) => v.foo), // Error accessing v.foo
    ]
};

We get a compiler error in the callback accessing v.foo that foo does not exist on type {} since {} is the default type when it is unable to infer the type. However, in this case we know from the Widget definition that T must be typed to {foo:number}. But, it appears TypeScript is not using this as an inference-site to properly determine T.

Activity

  1. RyanCavanaugh commented on Oct 14, 2015

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Oct 14, 2015
    Member
    More actions

    More simply:

    class Entry<T> {
        constructor(callback: (v: T) => number) { }
    }
    var x: Entry<{foo: number}> = new Entry(v => v.foo);
  2. Ryan Cavanaugh (RyanCavanaugh)
    changed the title [-]Type inference is not using a type declaration annotation as an inference-site.[/-] [+]Contextual typing does not flow backward through function return types to the function arguments[/+] on Oct 14, 2015
  3. Mohamed Hegazy (mhegazy)
    added
    QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
    on Oct 14, 2015
  4. RyanCavanaugh commented on Oct 14, 2015

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Oct 14, 2015
    Member
    More actions

    Let's go even simpler:

    function foo<T>(x: (n: T) => T): T { return x(undefined); }
    var x: string = foo(p => p.substr(2));

    (placeholder since I need to write a longer explanation)

  5. drarmstr commented on Oct 21, 2015

    @drarmstr
    Douglas Armstrong (drarmstr)
    on Oct 21, 2015
    Author
    More actions

    Mohamed Hegazy (@mhegazy), Sorry, what is the implication of the Question tag? Is there a follow-up question for me?

  6. Ryan Cavanaugh (RyanCavanaugh)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    In DiscussionNot yet reached consensusNot yet reached consensus
    and removed
    QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
    on Oct 21, 2015
  7. RyanCavanaugh commented on Oct 21, 2015

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Oct 21, 2015
    Member
    More actions

    Sorry, I forgot to write the follow-up I alluded to.

    After walking through this with the spec, I think the rule we would need to support this is that a function call (or constructor call) contextually typed by T creates an inference site corresponding to the return type of that function or constructor.

    That said, it's not clear what runtime semantics this rule corresponds to, or that it wouldn't be an undesirable breaking change.

  8. johnnyreilly commented on Oct 27, 2015

    @johnnyreilly
    John Reilly (johnnyreilly)
    on Oct 27, 2015
    More actions

    It would be fantastic if this could be looked into.

  9. RyanCavanaugh commented on Oct 31, 2016

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Oct 31, 2016
    Member
    More actions

    Moving discussion to #11152 which has clearer explanation

  10. Ryan Cavanaugh (RyanCavanaugh)
    closed this as completedon Oct 31, 2016
  11. Ryan Cavanaugh (RyanCavanaugh)
    added
    DuplicateAn existing issue was already createdAn existing issue was already created
    and removed
    In DiscussionNot yet reached consensusNot yet reached consensus
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    on Oct 31, 2016
  12. RyanCavanaugh commented on Sep 11, 2017

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Sep 11, 2017
    Member
    More actions

    I believe this is fixed by #16072

    Reacted by Aluan Haddad
  13. microsoft
    locked and limited conversation to collaborators on Jun 19, 2018
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

    DuplicateAn existing issue was already createdAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.