TypeScript error: Type 'string' is not assignable to union value
Hello friends.
I've picked up my side project again this week and I'm trying to add a new feature (required field indicator). It's a component library for forms. It's expecting specific form data:
When a user passes in the form data, they may or may not include the
requiredIndicator
. If they do, it can be either "TEXT", "ASTERISK", "CUSTOM" or a null value.
I thought I could tell TypeScript that these are the only 3 string values that it could be and have it work. But when I actually test it out, I get the following error:
Type 'string' is not assignable to type '"TEXT" | "ASTERISK" | "CUSTOM" | null | undefined'.
I've tried a bunch of things and can't seem to sort it out. I think the issue is that because I have no control over the data that the user is passing into the <Form> component, I can't use a type assertion.
I'm thinking that just typing it as 'string' is fine enough. I just thought that TypeScript could do this too.
I could stand to learn more, so I'm open to anything y'all have for me.
Thanks!Solution:Jump to solution
Typescript infers the types from the json to be plain types like string, and so you can assert type for it to be proper. To do so you can do the following, where Form is the type you defined
```ts
import data from "../data/query.json"
const mockData = data as Form...
7 Replies
It should work. How are you testing it? I mean, can you share the instance of where do you get that error?
On this branch (feature/typesafe-v1). Running
npm run start
spins up the example app in example/src/app.tsx
using data from example/data/query.json
https://github.com/TotalityWorks/wpgravitybundle/blob/feature/typesafe-v1/example/src/app.tsx
I get the error when I run start.
Full error message:
GitHub
wpgravitybundle/example/src/app.tsx at feature/typesafe-v1 · Totali...
A component and a hook to simplify your headless WordPress React application using Gravity Forms. - TotalityWorks/wpgravitybundle
Solution
Typescript infers the types from the json to be plain types like string, and so you can assert type for it to be proper. To do so you can do the following, where Form is the type you defined
You are exactly right.
I took a look at how I was using the package in the wild and realized I was doing exactly that, so past me must have known something that present me forgot:
Thanks for the help @shiroyasha9. You're the best
Glad to be here 🤝🏼
Actually @shiroyasha9, it fixed the compile issue I had but it isn't actually checking the type right.
If I have requiredIndicator as "TEXTs", it still works. Shouldn't that cause an error?
Type assertion is your way of telling typescript to shut up and assume that the type will always be what you mention. Simply speaking, it is the equivalent of "trust me bro".
We can do one of the following things here -
1. Add runtime validation check - use a library like
zod
where you define a schema (formSchema) and parse your data against that. It the data does not match schema, it throws an error which you can handle.
2. Convert the query.json file to a .ts file - This will help you define an object with the specified Form type.
This way typescript will properly check the values and show you an error if you have written a value incorrectly. The error will however not be shown at the place of usage, but rather where you define the data (query.ts) in this example.