Giter VIP home page Giter VIP logo

Comments (5)

fatcerberus avatar fatcerberus commented on April 28, 2024 1

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"

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on April 28, 2024

Indeed. See https://www.typescriptlang.org/docs/handbook/basic-types.html#any

from typescript.

starski avatar starski commented on April 28, 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.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on April 28, 2024

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

from typescript.

starski avatar starski commented on April 28, 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!

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.