-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Can't pass URL class object to fetch() in TypeScript #1695
Comments
According to the spec I couldn't find anything related to that the input should specifically accept a URL class. https://fetch.spec.whatwg.org/#request-class I would also like to pass URL to fetch/request but i genuinely feel like this is a typescript issue then anything else. You see some of this web api's have WebIDL conversion that normalize parameters to what it would like to have as an input parameter.
So you can (if you like to) just simply pass in an object, number, boolean, or literally anything you want. and WebIDL will try it's best to convert whatever you throw at it into a string. fetch({
toString() {
return 'https://httpbin.org/get'
}
}) // will fetch https://httpbin.org/get
fetch(true) // will fetch `./true`
fetch((blogId = 2)) // will fetch `./2`
fetch({}) // // will fetch `./[object%20Object]`
fetch({
[Symbol.toPrimitive](hint) {
console.log(hint)
if (hint === 'string') {
return 'https://httpbin.org/image/png'
}
return null
}
}) // will fetch https://httpbin.org/image/png and log the hint: 'string' And the URL class just so happens to have a So do i feel like we should add in each and every method like URL, boolean, number, bigint, toString and toPrimitive and so on? no /**
* @param { Request | any } input
* @param { RequestInit } init
*/
function fetch(input, init) { ... } Something even better would be to use /**
* @param { Request | USVString } input
* @param { RequestInit } init
*/
function fetch(input, init) { ... } but TS don't have any logic about USVString and how it can convert whatever into a USVString (it only have string support) it's the same argument about TS should actually have some kind of stringable USVString logic that we can use for ppl who mostly write polyfills microsoft/TypeScript#28775 |
Duplicate of microsoft/TypeScript#1261? But yeah, this is a though one. There is no special handling of TypeScript DOM typings does allow |
Thanks for your input guys, I believe this is a duplicate of microsoft/TypeScript#1261 indeed, so I'll reply in there, cheers! |
Reproduction
Steps to reproduce the behavior:
fetch(new URL(""));
tsc --noEmit
Argument of type 'URL' is not assignable to parameter of type 'RequestInfo'.ts(2345)
Expected behavior
Works as per example in README: https://github.com/node-fetch/node-fetch#manual-redirect
Screenshots
Your Environment
Additional context
Related:
#372
nodejs/node#17365
#1430
The text was updated successfully, but these errors were encountered: