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

The type of the return value is omitted. #57868

Closed
starski opened this issue Mar 20, 2024 · 5 comments
Closed

The type of the return value is omitted. #57868

starski opened this issue Mar 20, 2024 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@starski
Copy link

starski commented Mar 20, 2024

This will return an error, and that's how it's supposed to be:

const demoArray: number[] = [0,1,2,3,4,5];
const updatedArray: number[] = ['x', ...demoArray];
// Type 'string' is not assignable to type 'number'.

BUT somehow I managed to add 'x' to number[]
(I entered 'value :any' on purpose, it's the only way to indicate the problem/incorrect operation.)

function pushFirst(array: number[], value: any): number[]{
    return [value, ...array];
}
let demoArray: number[] = [0,1,2,3,4,5];
let updatedArray: number[] = pushFirst(demoArray, 'x');
console.log(
    demoArray,
    updatedArray
);

And in the console I have:

Array(6) [0, 1, 2, 3, 4, 5]
Array(7) ["x", 0, 1, 2, 3, 4, 5]
@starski starski added the Duplicate An existing issue was already created label Mar 20, 2024
@RyanCavanaugh
Copy link
Member

@starski
Copy link
Author

starski commented Mar 20, 2024

Sure, I understand that. The 'any' type is not the problem I am bringing to your attention.
Of course, I could provide the type number instead of 'any' and it would work as it should, but the behavior I encountered allows to break the contract.
The problem is that a variable that has a type specified as number[] and a function that has a declared return value also as number[] pass the string 'x' to value with type number[].

If this is intentional, I have a very hard time accepting it.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Mar 20, 2024

Your example errors unless you use any. The point of any is to not generate errors. It couldn't be more intentional.

@starski
Copy link
Author

starski commented Mar 21, 2024

However, I thought that:

  • 'any' allows the function to accept any type of a given parameter
  • return number[] ensures that regardless of the parameters the function takes, only number values will be provided in the returned array.

Thank you for your answers and patience.
Now I am aware that this can happen, although it is a bit unintuitive. I'll keep an eye out for this in my code.
Regards!

@fatcerberus
Copy link

If you want a type that means "any possible value" but is actually typesafe, you should use unknown instead of any. any basically means "don't typecheck this value in any context"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants