Consider the TypeScript code block below:
type URLEx = URL & {
custom: string;
};
const url = new URL("http://localhost:3000/foo/var");
const url_x: URLEx = {
...url,
custom: "hello",
};
console.log(url);
// Output properties of original URL object
console.log(url_x);
// Output properties of URL object with additional 'custom' property
Upon running the code, unexpected [Symbol(context)]: URLContext
and [Symbol(query)]: URLSearchParams
symbols were seen. Why did this happen? How can we transform the url
object into url_x
without manually specifying all properties so that the resulting output resembles the following?
console.log(url_x);
// {
// href: 'http://localhost:3000/foo/var',
// origin: 'http://localhost:3000',
// protocol: 'http:',
// username: '',
// password: '',
// host: 'localhost:3000',
// hostname: 'localhost',
// port: '3000',
// pathname: '/foo/var',
// search: '',
// searchParams: URLSearchParams {},
// hash: '',
// custom: 'hello'
// }