Coder Social home page Coder Social logo

Comments (3)

sinclairzx81 avatar sinclairzx81 commented on June 11, 2024 1

@eloyrobillard Hiya,

Going to close off this issue as this all appears to be standard (albeit interesting) Ts/Js behaviors.

Feel free to reply on this thread if you have any follow up questions though.
Cheers!
S

from typebox.

sinclairzx81 avatar sinclairzx81 commented on June 11, 2024

@eloyrobillard Hi,


First Bug

I think this is normal TypeScript behavior. Consider the following (without TypeBox) where there is an explicit guard for number types only, however the return value is inferred as any[].

const foos: any[] = [0, 1, 2];

const bars = foos.filter(foo => typeof foo === 'number') // any[]

However when passing a function, I believe TypeScript is able to correctly derive the return guard via Instantiation Expression. However it might be good to ping this question on the TypeScript repository (as this behavior is specific to TypeScript)

const foos: any[] = [0, 1, 2];

const check = (value: number): value is number => typeof value === 'number'

const nums = foos.filter(check) // number[]

Second Bug

The compiled TypeCheck<T> is an object type with internal fields that are accessed via this. As such, you won't be able to pass the Foo.Check function directly as the this context would be lost (which causes the error). However it is possible to .bind to ensure the context is retained. The following should work.

const foos: any[] = [0, 1, 2];

const Foo = TypeCompiler.Compile(Type.Number());

const nums = foos.filter(Foo.Check.bind(Foo)); // number[]

The above is a little awkward, but is standard JavaScript.

Hope this helps!
S

from typebox.

eloyrobillard avatar eloyrobillard commented on June 11, 2024

Thank you for the detailed reply

from typebox.

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.