Skip to content
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

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

Closed
Yhprum opened this issue Sep 29, 2024 · 5 comments
Closed
Labels
bug Something isn't working

Comments

@Yhprum
Copy link

Yhprum commented Sep 29, 2024

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 Yhprum added the bug Something isn't working label Sep 29, 2024
@Yhprum
Copy link
Author

Yhprum commented Sep 29, 2024

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

Repository owner deleted a comment Sep 29, 2024
gadicc added a commit that referenced this issue Sep 29, 2024
@gadicc
Copy link
Owner

gadicc commented Sep 29, 2024

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 🙏

gadicc pushed a commit that referenced this issue Sep 29, 2024
# [2.13.0](v2.12.5...v2.13.0) (2024-09-29)

### Bug Fixes

* **options:** re-export missing types ([#797](#797)) ([e96395f](e96395f))
* **setGlobalConfig:** cookieJar prop should be optional (fixes [#809](#809)) ([7e524fc](7e524fc))

### Features

* **quote:** export `QuoteField` type ([#808](#808)) ([a1f07d7](a1f07d7))
@gadicc
Copy link
Owner

gadicc commented Sep 29, 2024

Out in 2.13.0.

@Yhprum
Copy link
Author

Yhprum commented Sep 29, 2024

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!

@Yhprum Yhprum closed this as completed Sep 29, 2024
@gadicc
Copy link
Owner

gadicc commented Sep 29, 2024

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 🙏😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants