Skip to content

Context typing doesn't work for object literals #3570

Closed

Description

Context typing doesn't work for object literals. Simple example:

var a: {
    do1():(s:string) => void
}

a = {
    do1() { 
        return s => { 
            s. //should be string type
        }
    }
}

Activity

  1. DanielRosenwasser commented on Jun 19, 2015

    @DanielRosenwasser
    Member

    Strangely enough, this might be per spec:

    4.19 Contextually Typed Expressions

    • [...]
    • In a contextually typed object literal, each property value expression is contextually typed by
      • the type of the property with a matching name in the contextual type, if any, or otherwise
      • for a numerically named property, the numeric index type of the contextual type, if any, or otherwise
      • the string index type of the contextual type, if any.

    Notice that it doesn't mention method declarations, so the following actually will work:

    var a: {
        do1():(s:string) => void
    }
    
    a = {
        do1: () => { 
            return s => { 
                s. //actually is the 'string' type
            }
        }
    }

    The question is whether or not it should be. While methods are declarations, not expressions, this behavior leads to a pretty inconsistent experience. Anders Hejlsberg (@ahejlsberg) would be better suited to answer this.

  2. anstarovoyt commented on Jun 19, 2015

    @anstarovoyt
    Author

    Daniel Rosenwasser (@DanielRosenwasser) anyway this example works fine:

    var a: {
        do1(s:string):void
    }
    
    a = {
        do1(s) { 
            s. //actually is the 'string' type
        }
    }
    

    so context typing works for parameter types and doesn't work for return type.

  3. DanielRosenwasser commented on Jun 19, 2015

    @DanielRosenwasser
    Member

    Yes - actually I just noticed the following from the same section of the spec:

    • In the body of a function declaration, function expression, arrow function, method declaration, or get accessor declaration that has a return type annotation, return expressions are contextually typed by the type given in the return type annotation.

    So this is still per spec, but for a different reason. I'd still like to get Anders Hejlsberg (@ahejlsberg)'s take.

  4. anstarovoyt commented on Jun 19, 2015

    @anstarovoyt
    Author

    Daniel Rosenwasser (@DanielRosenwasser) I guess the main point is ' that has a return type annotation' in the part of spec.

    Notice: for me it is just not consistently:

    var a: {
        do1(s:string) => void    //   <-- context typing works for the parameter s
    }
    
    var a:() => (s:string)=>void    //   <-- context typing works for the return type 
    
    var a: {
       do1():() => (s:string)=>void //   <-- context typing for the return type doesn't work  
    }
    
  5. ahejlsberg commented on Jun 19, 2015

    @ahejlsberg
    Member

    Agreed, this is inconsistent. We already have it captured in #1610.

  6. 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 createdSpecIssues related to the TypeScript language specification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions