GithubHelp home page GithubHelp logo

Comments (4)

Xananax avatar Xananax commented on July 17, 2024 1

I confirm that changing the signature of numericString like so:

var numericString: (value: unknown) => value is `${number}`;

fixes the problem.

from is.

sindresorhus avatar sindresorhus commented on July 17, 2024

Maybe we could make the type guard more specific using a technique like this: https://github.com/sindresorhus/type-fest/blob/0b78096186dfe255b888513d60538e17e35828ea/source/internal.d.ts#L108-L111

from is.

Xananax avatar Xananax commented on July 17, 2024

Oh that sounds pretty good and seems like an easy change. Want me to PR this?

from is.

vtgn avatar vtgn commented on July 17, 2024

Use case:

const parseValue = (/**@type {string} */value) => {
  if (is.numericString(value)) {
    return parseFloat(value);
  }
  if (value.toLowerCase() === "true" || value.toLowerCase() === "false") {
    return value.toLowerCase() === "true";
  }
  //...
}

Typescript assumes value is a string and then casts value to never: image

I'm not sure if this is solvable, because strictly speaking the type guard is accurate.

Workaround (for anyone who needs it): recast the value manually:

JS:

value = /**@type {string}*/(value)

TS:

value = value as string

Notably, this recasting is possible in an early return context like my example, but not in an if...else or switch context, so it is still a bit of a bother.

The recasting is also surprising and requires a comment for other teammates that may come across it.

Hi!

The problem is that in a predicate function, the type guard must always be accurate: if the predicate on a given value returns true, the value must be of the given type in the type guard AND if the predicate returns false, the value must not be of the given type in the type guard.
And this is not the case in the is.numericString predicate. So the solution is indeed to modify the type guard to use a type representing all the numeric string values.

from is.

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.