Skip to content

Consider contextually typing class member functions by their base class/interface members #1373

Closed

Description

@kns98

here is my code

    export enum SortDir
    {
        Asc,
        Desc,
        Flip
    }

    export interface ISortInfo
    {
        getvalue(x: QuerySummary): any; //change to number|string in TypeScript 1.4
        order: SortDir;
        ordercalc?: number;
    }

    class SortByQueryId implements ISortInfo
    {
        getvalue = x => x.QueryId;
        order = SortDir.Flip;
    }

when I type "x." I expect to see the properties of QuerySummary but I do not. VS still thinks the type is 'any'

Activity

  1. danquirk commented on Dec 5, 2014

    @danquirk
    Member

    Contextual typing has only ever been applied to expressions and not statements so this has never worked. That said, it's certainly a reasonable thing to expect, at least for the function declaration style:

    class SortByQueryId implements ISortInfo {
        getvalue(x) { /* x should be QuerySummary in here */ }
    }

    Edited the title slightly to reflect this.

  2. changed the title [-]visual studio not showing context for lamba function[/-] [+]Consider contextually typing class member functions by their base class/interface members[/+] on Dec 5, 2014
  3. added
    Suggestion
    Needs Proposal
    on Dec 5, 2014
  4. kns98 commented on Dec 5, 2014

    @kns98
    Author

    Thanks Dan. I love this open community style development process :)

  5. jbrantly commented on Feb 3, 2015

    @jbrantly

    Can the scope of this be expanded to support member properties and static properties? See my comment in #1887 for an example.

  6. hdachev commented on Feb 10, 2015

    @hdachev

    Adding an example of broken return value inference that results in a compiler error:

    type Events = {
        [event: string]: () => void;
    };
    
    class A {
        getEvents(): Events {
            return null;
        }
    }
    
    class B extends A {
        getEvents() {
            return {
                'click': () => {
    
                }
            };
        }
    }
    
    // test.ts(12,7): error TS2415: Class 'B' incorrectly extends base class 'A'.
    //   Types of property 'getEvents' are incompatible.
    //     Type '() => { 'click': () => void; }' is not assignable to type '() => { [event: string]: () => void; }'.
    //       Type '{ 'click': () => void; }' is not assignable to type '{ [event: string]: () => void; }'.
    //         Index signature is missing in type '{ 'click': () => void; }'.
  7. omidkrad commented on Feb 19, 2015

    @omidkrad

    +1 vote for this. Related issues: #178 and #2073

    I think it's not intuitive for contextual typing not to apply when overriding a class member. i.e. it should.

  8. j-walker23 commented on Oct 19, 2015

    @j-walker23

    +1

  9. Coderah commented on Oct 31, 2015

    @Coderah

    +1

  10. added and removed
    Needs Proposal
    on Nov 3, 2015
  11. RyanCavanaugh commented on Nov 3, 2015

    @RyanCavanaugh
    Member

    Approved. When a method has multiple parent declarations (e.g. implementing multiple interfaces which each have a method named the same thing), union them to provide a contextual type.

  12. sandersn commented on Feb 1, 2016

    @sandersn
    Member

    This will be fixed by #6118.

  13. added
    Committed
    and removed on Feb 22, 2016
  14. tinganho commented on Apr 15, 2016

    @tinganho
    Contributor

    Can the scope of this be expanded to support member properties and static properties? See my comment in #1887 for an example.

    I'm also in support for extending it to base class members and statics.

  15. added
    Design Limitation
    Won't Fix
    on May 6, 2016
  16. sandersn commented on May 6, 2016

    @sandersn
    Member

    Unfortunately, we couldn't come up with a solution that was consistent and backward-compatible. See #6118 for details. I'm closing this for now.

  17. removed
    Committed
    on May 6, 2016
  18. locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Design LimitationSuggestionWon't Fix

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions