Type-checking methods do not accept `unknown`
See original GitHub issueAs 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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 toas any
.(For context, I am working in an environment which makes it a lint-violation to use the type
any
, sinceunknown
is a safer alternative.)@hen-zone @nicoespeon Released in zod@1.2.6