Comments (3)
@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.
@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.
Thank you for the detailed reply
from typebox.
Related Issues (20)
- Support for self-referencing fields of recursive type HOT 2
- Property '[Kind]' is missing in type 'TObject' but required in type 'TObject<TProperties>' HOT 3
- [feat] Bail / Early Cancel Mode HOT 1
- Setting Description HOT 1
- Schema dependency using Typebox HOT 3
- Composite working uncorrect HOT 5
- Include `Value.Errors` array on thrown errors HOT 2
- Add JSON path tracking to Encode/Decode HOT 2
- Is is it possible convert schema to json schema HOT 3
- No coercion in intersection of unions HOT 2
- typeboxCompiler error ENUM HOT 3
- `Composite` not working with union of object types HOT 11
- Value.Cast not working with an imported enum HOT 2
- Encoding does not ignore undefined values HOT 2
- Incorrect symbol escapement in record keys HOT 2
- Value.Hash doesn't work with Unicode strings HOT 3
- Given a type, convert it to Typebox HOT 2
- `TypeCheck<any>` results in `TS2589: Type instantiation is excessively deep and possibly infinite.` HOT 2
- `TypeSystem.Type` returning `any` after updating to `0.32.16` HOT 1
- Type.Schema HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typebox.