gadicc / node-yahoo-finance2

Unofficial API for Yahoo Finance
https://www.npmjs.com/package/yahoo-finance2
MIT License
374 stars 62 forks source link

Typescript error when passing an already declared array into `quote` fields #808

Closed Yhprum closed 18 hours ago

Yhprum commented 1 day ago

Bug Report

Describe the bug

Typescript error when passing a prebuilt array into quote fields

Minimal Reproduction

const YAHOO_QUOTE_FIELDS = ["regularMarketPrice"] as const; yahooFinance.quoteCombine("AAPL", { fields: YAHOO_QUOTE_FIELDS })

Environment

Browser or Node: node Node version (if applicable): 20.9.0 Npm version: 10.1.0 Browser verion (if applicable): Library version (e.g. 1.10.1): 2.12.5

Additional Context

since the quote fields are the list of options and not a generic string[], I can't pass an array of options in like I was previously (this is good though, I like the stricter typing of options. However the way I generaly deal with this is adding as const after the array to make the type the literal strings instead of a generic string array, but I get this error now. The type <myType> is 'readonly' and cannot be assigned to the mutable type <yourType>

Yhprum commented 1 day ago

maybe we could add a QuoteFields type that I could add to the array like const YAHOO_QUOTE_FIELDS: QuoteFields = [...

you can close this if it's a duplicate of #807, not 100% sure

gadicc commented 1 day ago

Hey, @Yhprum. I think we need the as const for the string part, no? e.g.

- const YAHOO_QUOTE_FIELDS = ["regularMarketPrice"] as const;
+ const YAHOO_QUOTE_FIELDS = ["regularMarketPrice" as const];

That fixes the error for me.

However, I also like your idea of exporting QuoteField (especially since you'll get nice text completion in your editor), so that will be in the next release:

import type { QuoteField } from "yahoo-finance2/dist/esm/src/modules/quote";
const YAHOO_QUOTE_FIELDS: QuoteField[] = ["regularMarketPrice"];

Thanks for raising this :pray:

gadicc commented 1 day ago

Out in 2.13.0.

Yhprum commented 18 hours ago

Thanks a bunch for this (and my other issues)! This works great with the exported type. The reason I didn't want to to the as const for each string is I have an array of ~20 fields I include in the request and it was super messy adding them to each one.

Appreciate the quick response!

gadicc commented 16 hours ago

Hey, sure, and thanks for reporting back! These are all a few quirks that slipped through our new typescript / validation architecture so very grateful to have the reports and get to crunch them asap. So thanks again 🙏😁