dohomi / storyblok-generate-ts

Generates TypeScript interface types based on Storyblok component file
MIT License
101 stars 32 forks source link

Should numeric fields really have type string? #46

Closed scmx closed 5 months ago

scmx commented 1 year ago

Hi 👋 I noticed the recent change "Parse numbers to string type" #45 to change numeric fields to have type string in the resulting interface. This might not be correct. For me the numeric field values have always been given as number literals from api and storyblok-js-client.

Perhaps the data you looked at was a field that had type string and was changed to numeric, and there might be some bug resulting in you continuing to get it as a string because of the previous type?

Some ideas: Maybe a jsdoc comment could be generated that shows the storyblok type of each field.

If we can verify that the value is sometimes a number then maybe consider having string | number as generated type. Consumers would still need to wrap each usage with Number(blok.field) but at least you see that it might already be a string.

dohomi commented 1 year ago

As discussed in PR https://github.com/dohomi/storyblok-generate-ts/pull/45 @davidhoeck changed it to string. I am honestly not sure whats the right approach.. currently I cast a lot of string => number but its been a while since I worked with Storyblok

dlhck commented 1 year ago

I created a new number field and with every configuration the output was a string. So I don't think that Storyblok returns int or float values at all.

dlhck commented 1 year ago

An example:

The JSON:

image

The Field:

image

The Field config:

image
bilal1133 commented 5 months ago

Any progress on this, Please help.

dohomi commented 5 months ago

you have to manually cast your number fields inside your components from string => number. Thats currently the only way as the response from the Storyblok API is of type string.