Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[] passes Record check #32

Closed
timm-gs opened this issue Dec 21, 2017 · 4 comments
Closed

[] passes Record check #32

timm-gs opened this issue Dec 21, 2017 · 4 comments

Comments

@timm-gs
Copy link

timm-gs commented Dec 21, 2017

I have a type like the following specified:

const ConfigType = rt.Record({})
.And(rt.Partial({
  x: rt.String,
}));

Unfortunately running the following code succeeds without error:

ConfigType.check([])
@pelotom
Copy link
Collaborator

pelotom commented Dec 21, 2017

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?

@timm-gs
Copy link
Author

timm-gs commented Dec 22, 2017

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(`[]`))

@pelotom
Copy link
Collaborator

pelotom commented Dec 22, 2017

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.

@timm-gs
Copy link
Author

timm-gs commented Jan 8, 2018

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.

@timm-gs timm-gs closed this as completed Jan 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants