Skip to content

[Spec] contextual typing of return expression values #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhegazy opened this issue Jul 17, 2014 · 2 comments
Closed

[Spec] contextual typing of return expression values #72

mhegazy opened this issue Jul 17, 2014 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification

Comments

@mhegazy
Copy link
Contributor

mhegazy commented Jul 17, 2014

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

@mhegazy mhegazy added this to the TypeScript 1.1 milestone Jul 17, 2014
@JsonFreeman
Copy link
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.

@ahejlsberg
Copy link
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.

@ahejlsberg ahejlsberg changed the title [Errors] contextually typed return types of array types [Spec] contextually typed return types of array types Jul 17, 2014
@mhegazy mhegazy added the Spec label Jul 17, 2014
@sophiajt sophiajt changed the title [Spec] contextually typed return types of array types [Spec] contextually typed return types of function expression types Jul 18, 2014
@sophiajt sophiajt changed the title [Spec] contextually typed return types of function expression types [Spec] contextual typing of return expression values Jul 18, 2014
@RyanCavanaugh RyanCavanaugh added the Fixed A PR has been merged for this issue label Sep 11, 2014
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification
Projects
None yet
Development

No branches or pull requests

5 participants