Answer the question
In order to leave comments, you need to log in
How correctly to register types for such function?
The following function is written in JS:
/**
* @param {URL | string} url
* @param {boolean} returnAsURL
* @return {URL | string}
*/
function clearURL (url, returnAsURL = url instanceof URL) {
url = new URL(url)
// ...
return returnAsURL ? url : url.toString()
}
.d.ts
a file so that VS Code clearly understands what type of data will be returned?
Answer the question
In order to leave comments, you need to log in
Overloads will help . Something like that:
export function clearURL (url: URL): URL;
export function clearURL (url: string): string;
export function clearURL (url: URL | string, returnAsURL: true): URL;
export function clearURL (url: URL | string, returnAsURL: false): string;
export function clearURL (url: URL | string, returnAsURL: boolean): URL | string;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question