-
Notifications
You must be signed in to change notification settings - Fork 88
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
Incorrect return type for CompletedBody.getJson #155
Comments
There's a set of separate types in types.ts that are intended to cover the most important & explicitly public parts of the API interface (i.e. the core types people can depend on) in one place. For this case there's only one implementation, but for most of the other types there that's not true. Notably every exported type in there is re-exported directly from
I'd very very strongly prefer to avoid introducing It's better than downstream users are explicit about what they expect here (including
Sure, but this method can still return undefined. It will do so if the JSON is not parseable, or if the body is not decodeable at all (e.g. corrupted gzip, unknown encoding) - that's what Anyway, yes, the The method itself should probably be generic with a default parameter too, so you can easily set an explicit type if you want to. microsoft/TypeScript#1897 has a lot of similar related discussion. I think in this case, we could keep it simpler & stricter: rather than trying to come up with a fully flexible type we'd just provide a convenient base type, and encourage asserting to the expected type directly from there instead. For example that could be just That could create some breakage, but I think only in some very specific cases like All put together, that would give us something like: getJson<R = {} | [] | string | number | boolean | null>: Promise<R | undefined> Would that work for you? If so, a PR to add this and some example tests to cover a few of the relevant cases would be welcome, feel free! |
Good point (and likely true considering their partial move from any to unknown on caught values in a trycatch).
Oops, sorry - looks like I didn't look at this properly
I don't think this does quite what you think it does. let x: {} = 1; is valid (Video Explainer) I don't believe any of the issues mentioned in the original issue post you linked apply for this usecase since the inferred return type of the function is If typed as unknown, it should likely be added to the doc comment that I generally like to contribute, but I don't feel too confident adding tests to a project that im not that familiar with ^^' |
Ok, fair enough! Personally this doesn't seem like a huge problem and I'm super busy right now, so it's unlikely I'm going to look into this in more depth in the short term myself. I'll keep an eye on it for future in case it is causing big problems for more users, or do feel free to open a PR in the meantime if this is important to you. |
Implementation
mockttp/src/util/request-utils.ts
Lines 233 to 237 in b50532a
Types (why are these even separate?)
mockttp/src/types.ts
Lines 176 to 181 in b50532a
JSON.parse can not return undefined (but can return null, which is not the same) and can return more than just
object
s (numbers, strings).Probably best to just type getJson as Promise just like JSON.parse is typed as any.
The text was updated successfully, but these errors were encountered: