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.

Use `unknown` as the default any type rather than literal `{}`

See original GitHub issue

Context

Referencing the following code:

https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/8105f2b51dd62241f6daae8abc97675374fae5b7/src/zod/index.ts#L36

Currently, with a custom scalar Date: Date (see graphql-codegen scalarMap config) we end up with type errors such as "{} is missing properties from Date toLocaleString, ..." etc.

Proposal

Replace {} with unknown would allow consumers of this library to cast as neccessary.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
chrisuicommented, Feb 11, 2022
1reaction
chrisuicommented, Feb 11, 2022

Hey, sorry for delay in reply.

I am passing scalars like the following into my codegen configuration block:

Date: 'Date',
Height: 'ProjectNamespace.Height',
Weight: 'ProjectNamespace.Weight',

In log output I see the following:

unhandled name: Height
unhandled name: Weight
unhandled name: Date

Upon inspecting the generated code anything that was using the Height or Weight scalars are now defined as definedNonNullAnySchema.

When I use this schema I cannot actually pass the shape of these scalars as I’ve defined because of type errors "{} is missing properties from Date toLocaleString, ...".

Ie. The following won’t work

type example {
  dateOfBirth: Date!
}
const schema = z.object({dateOfBirth: definedNonNullAnySchema})
const data = schema.parse({dateOfBirth: new Date()})
const dateString = data.dateOfBirth.toLocaleString()

So just to confirm, I think that there are two issues: the default {} as “any” doesn’t really provide any flexibility for fallbacks as even the generated code will not compile. There ideally needs to be a way to provide a generator for custom scalars.

Ps. Sorry for additional notifications as I accidentally submitted early and closed the issue by mistake.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - why can I assign a literal with unknown properties ...
typescript - why can I assign a literal with unknown properties to a variable of type {} - Stack Overflow. Stack Overflow for...
Read more >
The unknown Type in TypeScript - Marius Schulz
TypeScript 3.0 introduced a new unknown type which is the type-safe counterpart of the any type.
Read more >
Statically Type Unknown Values with TypeScript's unknown ...
In this lesson, we'll go over TypeScript's unknown type. The unknown type is the type-safe counterpart of the any type. Both unknown and...
Read more >
Handbook - Literal Types - TypeScript
There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an...
Read more >
no-object-literal-type-assertion - Rule
Disallow object literals to appear in type assertion expressions (default). Casting to `any` and `unknown` is allowed. · Allow using a type assertion...
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