Skip to content

[Spec] contextual typing of return expression values #72

Closed

Description

interface IBookStyle {
    initialLeftPageTransforms?: (width: number) => NamedTransform[];
}

interface NamedTransform {
    [name: string]: Transform3D;
}

interface Transform3D {
    cachedCss: string;
}

var style: IBookStyle = {
    initialLeftPageTransforms: (width: number) => {
        return [
            { 'ry': null }
        ];
    }
}
//!!!Type '{ initialLeftPageTransforms: (width: number) => { 'ry': any; }[]; }' is not assignable to type 'IBookStyle':
//!!!Types of property 'initialLeftPageTransforms' of types '{ initialLeftPageTransforms: (width: number) => { 'ry': any; }[]; }' and 'IBookStyle' are incompatible:
//!!!Call signatures of types '(width: number) => { 'ry': any; }[]' and '(width: number) => NamedTransform[]' are incompatible:
//!!!Types of property 'pop' of types '{ 'ry': any; }[]' and 'NamedTransform[]' are incompatible:
//!!!Call signatures of types '() => { 'ry': any; }' and '() => NamedTransform' are incompatible:
//!!!Index signatures of types '{ 'ry': any; }' and 'NamedTransform' are incompatible.

Expected: errors as above

Actual: no errors

Activity

  1. JsonFreeman commented on Jul 17, 2014

    @JsonFreeman
    Contributor

    This is because of the type annotation on width, causing the return expression to not be contextually typed. It might make sense for the return expression to be contextually typed, even in the presence of parameter annotations.

  2. ahejlsberg commented on Jul 17, 2014

    @ahejlsberg
    Member

    Changing to spec issue. In the old compiler we would contextually type the return value of a function expression only if the function expression had no parameter or return type annotations. The new compiler contextually types the return value when the function expression has no return type annotation, regardless of whether there are parameter type annotations. We previously discussed this and agreed we want the new behavior. Spec sections affected are 4.9.3 and 4.19.

  3. changed the title [-][Errors] contextually typed return types of array types[/-] [+][Spec] contextually typed return types of array types[/+] on Jul 17, 2014
  4. changed the title [-][Spec] contextually typed return types of array types[/-] [+][Spec] contextually typed return types of function expression types[/+] on Jul 18, 2014
  5. changed the title [-][Spec] contextually typed return types of function expression types[/-] [+][Spec] contextual typing of return expression values[/+] on Jul 18, 2014
  6. 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

BugA bug in TypeScriptFixedA PR has been merged for this issueSpecIssues related to the TypeScript language specification

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions