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

Need help with typescript typings #6

Open
skjnldsv opened this issue Jun 23, 2023 · 7 comments
Open

Need help with typescript typings #6

skjnldsv opened this issue Jun 23, 2023 · 7 comments

Comments

@skjnldsv
Copy link

Hey @perry-mitchell

I'm patching the request method of your webdav lib, and typescript is complaining.
Do you have any tips on how to comply to this? I think this is an issue or misunderstanding from my part on the PatchFn type :)

    /**
     * Allow to override the METHOD to support dav REPORT
	 *
	 * @see https://github.com/perry-mitchell/webdav-client/blob/8d9694613c978ce7404e26a401c39a41f125f87f/source/request.ts
	 */
	const patcher = getPatcher()
	patcher.patch('request', (options: RequestOptions) => {
		if (options.headers?.method) {
			options.method = options.headers.method
			delete options.headers.method
		}
		return request(options)
	})
	return client

image

@perry-mitchell
Copy link
Owner

Hey @skjnldsv! Hmm, that's weird, it seems to work for me:

image

Is your typescript version up to date?

@skjnldsv
Copy link
Author

typescript 4 or 5, both are stating the same :)
I'm seeing this on my Code IDE 🤔

@perry-mitchell
Copy link
Owner

perry-mitchell commented Jun 27, 2023

I wonder if your IDE is using some other version of typescript?

Can you build with such a change? That should show if the version installed as a dependency is correct or not..

EDIT: It shouldn't I don't think, as you're using vscode..

@skjnldsv skjnldsv reopened this Jun 27, 2023
@skjnldsv
Copy link
Author

(wrong click)

Weirdly, I can build, but I don't know what tool the (vs)code extension is using, i'll investigate.

@skjnldsv
Copy link
Author

Let's close, can't find anything 🤷

@skjnldsv skjnldsv reopened this Jun 29, 2023
@skjnldsv
Copy link
Author

Actually, jest complains too 😁

    apps/files/src/services/WebdavClient.ts:45:27 - error TS2345: Argument of type '(options: RequestOptions) => Promise<Response>' is not assignable to parameter of type 'PatchFn<Promise<Response>>'.
      Types of parameters 'options' and 'args' are incompatible.
        Type 'unknown' is not assignable to type 'RequestOptions'.

    45  patcher.patch('request', (options: RequestOptions): Promise<Response> => {
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@susnux
Copy link

susnux commented Jul 25, 2023

This will now work as the patch function is declared as

patch<T>(key: string, method: PatchFn<T>, opts?: PatchOptions): this;
//...
export type PatchFn<T> = (...args: Array<unknown>) => T;

So when passing (options: RequestOptions): Promise<Response> Typescript must convert the args of type unknown to RequestOptions but this is not allowed.
As you can not assign unknown to any other type without explicit casting.

The correct type would be

export type PatchFn<T> = (...args: Array<any>) => T;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants