question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type-checking methods do not accept `unknown`

See original GitHub issue

As of version 1.2.5, the parse, is and check methods on ZodType expect to receive an argument which is already of the schema’s type. This is surprising to me; at version 1.0.15 I was passing unknown values to these functions, since the input is of unknown type (typically from IO) and the functions are being called explicity to discover whether the value matches the type.

Since I don’t want to incorrectly-type the values before they are checked, it seems the only option here is to use any, which from my point of view is strictly worse than unknown.

Is there a downside I’m not seeing to permitting unknown as input to these functions?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
hen-xcommented, Apr 22, 2020

@vriad I think it would be a good compromise to have the methods accept T | unknown. That way we can reject any obviously-incorrect values (ie those for which we already have an incompatible type), while allowing users to supply unknown values without resorting to as any.

(For context, I am working in an environment which makes it a lint-violation to use the type any, since unknown is a safer alternative.)

1reaction
colinhackscommented, Apr 22, 2020

@hen-zone @nicoespeon Released in zod@1.2.6

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python type checking: cannot assign to a dict - Stack Overflow
Argument of type "dict[Unknown, Unknown]" cannot be assigned to parameter ... "method": method} if params is not None: reqDict["params"] ...
Read more >
The unknown Type in TypeScript - Marius Schulz
Let's assume we want to write a function that reads a value from localStorage and deserializes it as JSON. If the item doesn't...
Read more >
1. Typechecking - Hack and HHVM [Book] - O'Reilly
The typechecker understands that code after a call to a noreturn function or static method (but not a non-static method) can never be...
Read more >
Common issues and solutions - mypy 0.991 documentation
This section has examples of cases when you need to update your code to use static typing, and ideas for working around issues...
Read more >
Type checking - Luau
Unlike any , unknown will not allow itself to be used as a different type! local function unknown(): unknown return if math.random ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found