-
Notifications
You must be signed in to change notification settings - Fork 44
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
Improve legibility of the shape mismatch error #93
Comments
The suggested phrasing works equally well regardless of input type or the type of the shape. |
It would be nice to have a console error that would explain the mismatch, e.g.
Though thats not straightforward. I wonder if there is an existing package that could be adapted for the purposes – diffing object values/ types. |
Totally agree but as you said, offering those detailed runtime errors is not so straightforward and will require refactoring the plugin a bit because right now we don't keep track of which particular check failed, we just generate something like: if (!(typeof thing === "object" && typeof thing.foo === "function" && typeof thing.bar === "boolean")) {
throw new TypeError("something is wrong!")
} When what we really need is something like: if (!(typeof thing === "object" && thing !== null)) {
throw new TypeError("thing must be an object, got " + inspect(thing));
}
else if (!(typeof thing.foo === "function")) {
throw new TypeError("thing.foo must be a function, got " + inspect(thing.foo));
}
else if (!(typeof thing.bar === "boolean")) {
throw new TypeError("thing.bar must be a boolean, got " + inspect(thing.bar));
} etc. I'm pretty doubtful that an existing lib can be adapted for this, and we wouldn't gain much from it anyway. The important bit is that we retain control over where the error instance is created in order to preserve a sensible stack trace. |
Almost there https://github.com/gajus/pretty-print-object#annotate-value-types |
See https://github.com/gajus/babel-plugin-typecheck-annotated-difference for an implementation example using |
@gajus like it but there are some problems to consider:
I think the approach we'll have to take here will be a combination of the existing checks and some of the techniques from your library. I have some ideas for this, will PR them tonight. |
That is a question for you. There must be a way (at a babel plugin level) to get the type FooType = {
a: string,
b: string,
c: Array<string>
}; needs to be fed to my script in a form of: {
a: 'string',
b: 'string',
c: 'Array<string>'
};
It makes no difference. We are only comparing the types, not the values themselves.
It does not. Circular reference cannot exist in a type declaration. It does not matter if circular reference exists in the subject object.
You bundle it as a Here is an example of me doing this in another babel plugin, https://github.com/gajus/pragmatist/blob/a04832b9aa97887691bd1d782b28cec8de5c5524/src/babel-plugin-source-map-support/index.js#L34. This approach assumes that |
They can, e.g. this is valid: type Tree = {
value: any;
parent?: Tree;
left?: Tree;
right?: Tree;
};
If we're doing this I think that we'll need to inline the pretty print module into the script to avoid that kind of user pain, in the same way that babel generates a One more point - how do you handle e.g. type state = "active" | "inactive";
// becomes:
var state = '"active"|"inactive"';
// and not:
var state = 'active|inactive'; |
For the record, the library has been renamed to |
It can be either. |
Wouldn't you need to inline it in every file then? |
Yes, but this is acceptable, I added this for the current "inspector" in #94 |
You are right in the sense that there isn't that much of the code. Anyway, I am happy for you to proceed with the inline approach. How can I help? |
@phpnode have you had time to look into this? |
Fixed in 3.6.0 but can probably be further improved. |
Referring to the example from the documentation, https://github.com/codemix/babel-plugin-typecheck#shape-tracking.
The error is completely unreadable to the user:
First of all,
is not an instance of
User
. The error ("expected User got Object") makes it look like the object needs to be constructed usingUser
constructor function.Therefore,
would be a good start.
The text was updated successfully, but these errors were encountered: