-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resolving readJson
to something other than unknown
#6438
Comments
I think This is not a place to use generics, see #5738 (comment). |
I think I just realized that the type assertion/cast can be done inline so maybe this isn't as much of an issue as I originally thought const data = await readJson(filePath) as Data; closing but feel free to reopen if there's more to do here thank you |
In a file, it would load and parse and return as a number. Also, the type for |
current types for
JSON.parse
returnany
so when parsing JSON it is easy to tell TS what type the parsed JSON is:readJson
, however, resolves tounknown
so things are a bit more complicated:this can be worked around but I think it makes the code clunky:
I propose one of the following:
change
readJson
to resolve toany
(to be consistent withJSON.parse
, some could argue thatJSON.parse
shouldn't returnany
though)change
readJson
to resolve toT
whereT = unknown
and can be specified or inferred:or
same as 2. except
T
defaults toany
(for consistency withJSON.parse
)personally I like 2. but
unknown
is inconsistent with current type definitions ofJSON.parse
and I typically favor consistency so I suggest 1. or 3.The text was updated successfully, but these errors were encountered: