Skip to content
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

Function assignment compatibility differs from spec. #3080

Closed
ghost opened this issue May 8, 2015 · 2 comments
Closed

Function assignment compatibility differs from spec. #3080

ghost opened this issue May 8, 2015 · 2 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@ghost
Copy link

ghost commented May 8, 2015

Consider this:

var foo: (number) => void = (bar: string) => bar.substr(0);

According to spec, 3.10.4 Assignment Compatibility:

  • M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any),
    • the signatures are of the same kind (call or construct)
    • M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M
    • for parameter positions that are present in both signatures, each parameter type in N is assignable to or from the corresponding parameter type in M, and
    • the result type of M is Void, or the result type of N is assignable to that of M.

Here, M = (number) => void, N = (string) => string. This is not a case of function argument bi-variance as in #2170, for string isn't assignable to or from number, and the assignment should fail to compile. Meanwhile, neither the playground nor TypeScript 1.5 for VS2013 complains.

Is this a bug in the compiler, or the spec is to be updated?

@DanielRosenwasser DanielRosenwasser added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label May 8, 2015
@DanielRosenwasser
Copy link
Member

This is an unfortunate consequence of the way function type literal syntax works in our language. In a function type literal, parameter names are mandatory and the types are optional. So in your example, you don't have a parameter of type number, you have a parameter named number of type any. Here, you can try it out on the TypeScript Playground where you'll see the correct error.

@DanielRosenwasser
Copy link
Member

For the record, if you use TypeScript with the --noImplicitAny flag, we do catch this problem.

@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
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

1 participant