TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

add URLPattern type in DOM or something lib?

Open sinoon opened this issue 3 years ago • 4 comments

lib Update Request

Configuration Check

My compilation target is ESNEXT and my lib is the default.

Missing / Incorrect Definition

URLPattern is enabled by default in Chrome and Edge version 95 and above.

But TypeScript missing the definition

Sample Code

new window.URLPattern({
    pathname: '/:a/:b',
})

image

Documentation Link

doc: https://web.dev/urlpattern/

sinoon avatar Nov 07 '21 09:11 sinoon

Per MDN only Chromium enabled that feature yet. https://developer.mozilla.org/en-US/docs/Web/API/URLPattern#browser_compatibility

saschanaz avatar Nov 11 '21 12:11 saschanaz

It would be really awesome to also get a utility type that produces the correct groups types based on the pattern string. So we could write code like:

const pattern = new URLPattern('/one/:foo', document.baseURI);
const params = pattern.exec('http://localhost/one/bar').pathname.groups;
console.log(params.foo); // no error
console.log(params.bar); // error

justinfagnani avatar Nov 19 '21 17:11 justinfagnani

Xref https://github.com/denoland/deno/issues/12558

lucacasonato avatar Dec 09 '21 19:12 lucacasonato

For reference, there's a polyfill that comes with types. You just have to import it without assignments so it's available globally, including the types.

https://github.com/kenchris/urlpattern-polyfill/blob/main/src/types.d.ts

You can alternatively add this line to your global.d.ts to just import its types:

/// <reference types="urlpattern-polyfill" />

fregante avatar Jun 05 '22 16:06 fregante