Comments (4)
The corresponding static type failure is
type ConfigType = { x?: string }
const foo: ConfigType = []
// ^^^ Type 'undefined[]' has no properties in common with type '{ x?: string; }'.
Are you saying you'd like a corresponding runtime check to this?
from runtypes.
Yes, sorry I omitted that part. I'm using runtimes
in this case to validate a config file loaded from JSON, and that's where it fails.
So the full sample code would be:
ConfigType.check(JSON.parse(`[]`))
from runtypes.
Just to be clear, though, this type of check would not flag an error if your ConfigType
had any field that is present on arrays. For example:
type ConfigType = { x?: boolean, length: number }
const foo: ConfigType = []
This works in the static type system because ConfigType
and Array
are compatible and share a common property, length
. If your goal is to outlaw the use of arrays for a ConfigType
, even though they're technically compatible, you might want to use a Constraint
. Note that the rt.Record({}).And(...)
in your original example is superfluous, saying that something is an empty record is just saying that it could be literally anything except null
or undefined
, not precluding the possibility that it is an array.
from runtypes.
Ok, so if this is by design I'll close this.
I have already added an extra check to verify that it's an object. So that seems settled then.
from runtypes.
Related Issues (20)
- Add TypeScript compatibility table HOT 1
- `Union` type checks successfully when all members individually do not when using `Intersect`
- `Union` validate wrongly validates object when the runtypes have the same property and one of the properties is a `Union`
- Is there a way to omit everything that's not defined by the Record instance? HOT 1
- Create runtypes from existing TypeScript types? HOT 1
- Printable form of type from runtype? HOT 1
- Unuseful error message on typechecking HOT 1
- Cannot validate optional+null circulare fields HOT 4
- Optional types in Tuple HOT 1
- How check for ObjectId? HOT 3
- Function which accepts a runtype and omits some fields HOT 1
- `omit` properties of a `Constraint` or `Intersect`? HOT 2
- Constraint::reflect does not expose .underlying
- Support for Set type HOT 2
- [Feature Request] Type Guard for ValidationError HOT 2
- Option to return errors instead of throwing them for performant validation. HOT 4
- Runtypes-like `.guard()` HOT 1
- How do I exclude undefined properties from record? HOT 2
- Is it possible to have more than one brand HOT 2
- Type validated by Dictionary is different than returned type
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 runtypes.