question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

USVString type definition

See original GitHub issue

TypeScript Version: 3.2.0-dev.201xxxxx

Search Terms: USVString

Code

function example(uri: USVString) {
    // Do some work
}

Expected behavior: Until typescript version 3.0.3 USVString used to be an existing type defined in lib.dom.d.ts in version 3.1, according to the breaking changes page, some vendor-specific types are removed from lib.d.ts, a full list of removed types is included. USVString is not on that list, yet it’s type definition was also removed.

USVString is defined under webIDL, as can be seen here.

Actual behavior: error TS2304: Cannot find name 'USVString'.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jimmywartingcommented, Jun 27, 2021

I have been pretty much bothered with the way typescript handles USVString as a String To me they don’t seem to be the same thing

a USVString dose lots of conversion casting mostly anything to a string, it deals with Symbol.toStringTag, toString() and defaulting values by doing something like String(null_or_whatever)

Here are some example

// This works fine and it's totally valid in vanilla js!
async function get (req) {
    const url = new URL(req)
    url.searchParams.set('api_key', '---')
    return fetch(url)
}

TS throws for using fetch in this way…

Argument of type 'URL' is not assignable to parameter of type 'RequestInfo'.
  Type 'URL' is not assignable to type 'string'.

https://github.com/microsoft/TypeScript/blob/cec2fda9a53620dc545a2c4d7b0156446ab145b4/lib/lib.dom.d.ts#L19650

it shouldn’t be a regular string. It should be a USVString instead!

Same thing with blobs! it’s totally legit to do: new Blob([{}]) if you want to do something like this, you could provide a own custom class that has a toString method and have it working

- type BlobPart = BufferSource | Blob | string;
+ type BlobPart = BufferSource | Blob | USVString;
1reaction
weswighamcommented, Nov 30, 2018

Our IDL-based generator just interprets USVString as an alias for string, because it effectively is. Do you still need a type USVString = string alias in the lib? The lib itself stopped using it because it was rather pointless.

Read more comments on GitHub >

github_iconTop Results From Across the Web

USVString type definition · Issue #28775
Some of our modules as well as some external NPM modules we are using need USVString as a type. Even though I agree...
Read more >
USVString
USVString corresponds to the set of all possible sequences of unicode scalar values. USVString maps to a String when returned in JavaScript; ...
Read more >
USVString - Web APIs - W3cubDocs
A USVString is a sequence of Unicode scalar values. This definition differs from that of DOMString or the JavaScript String type in that...
Read more >
USVString - Web APIs | MDN - Web technology for developers
A USVString is a sequence of Unicode scalar values. This definition differs from that of DOMString or the JavaScript String type in that...
Read more >
String is USV String details
The WebAssembly Interface Types proposal also restricts string values to lists of Unicode Scalar Values, as polled in a recent CG meeting. Interfacing ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found