Given this TypeScript snippet:
type URLEx = URL & {
custom: string;
};
const url = new URL("http://localhost:3000/foo/var");
const url_x: URLEx = {
...url,
custom: "hello",
};
console.log(url);
// {
// 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: ''
// }
console.log(url_x);
// {
// custom: 'hello',
// [Symbol(context)]: URLContext {
// flags: 400,
// scheme: 'http:',
// username: '',
// password: '',
// host: 'localhost',
// port: 3000,
// path: [ 'foo', 'var' ],
// query: null,
// fragment: null
// },
// [Symbol(query)]: URLSearchParams {}
// }
I was not expecting to see [Symbol(context)]: URLContext
and [Symbol(query)]: URLSearchParams
. Why is that so? And how can I convert url
into url_x
without manually mapping all properties so logging the latter would produce 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'
// }