-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 return type with array #33158
Comments
variableList.map((v) : { name?: string; type: string } => (
{ nasme: v.name, type: v.type }
)); |
You're running into this thing called excess property checks for object literals. It isn't applied on non-object literals. Without the explicit return type annotation on your Then, the result of |
And... |
It knows the type. It just isn't applying the excess property checks because it wasn't made to do so. One of these may help, Also, try looking up "excess property check" and "object literal". Might also include other workarounds you might find more agreeable |
What I believe happens here is that
If you want the excess property checks in the lambda, annotate its return type: (v): ReturnType => value |
But typescript as far as I understood already infers return type of callback. It knows exactly what map will return. |
Even if types are different depending on conditions, it's quite possible to return union type... :) |
It looks like #40311 fixes this. Consider [1,2,3].map<{a:1}>(x => ({a:1, b:2})); We want the excess property Current (v4.0.2) Playground -- no error PR Playground -- gives "may only specify known properties" error. 🎉 🎉 🎉 |
TypeScript Version: 3.6.2
Search Terms:
map\array\return type
Code
Expected behavior:
Typescript complaints that array of objects generated by
map
doesn't match function return type.Actual behavior:
Typescript doesn't complaint that array of objects generated by
map
doesn't match function return type.Playground Link:
http://www.typescriptlang.org/play/#code/PTAEGUAsHsFcBsAmoDG0C2AHeBDAlgHYBQaBAzgC6gBmAphSpAMKyUYBqOATnjgEbxaZUAF5QACgCUALlABvUARzpaAflmUeBAOYBuUBQCemNRopbtoAL4BtALqiAfPKKg3qaOSoA3brwG0ADJ4lLIKSirqoJqEegbGtGYW1vaioPa6ru5c9LBcBKC+PPyCwZQAdOg4mOLeThLhOGQqst7lEbQANPEmreVGJtaSkplWmUQgoACCwpC0OSSelDT0jCxs6JzFAWQATGlSYYrKptHmsfoDpzE61vVyWW6ky0X+pSEURx1RN3FXSbEUg4xBlHqAchQ8gVGs1EqAAOQARnh3X+CN28LGRCxQA
Related Issues:
No
The text was updated successfully, but these errors were encountered: