Typescript: Missing escape/unescape ?

Created on 17 May 2016  ·  14Comments  ·  Source: microsoft/TypeScript

TypeScript Version:

1.8
Code

var a = escape("&#");
var b = unescape(a);

Expected behavior:
no error

Actual behavior:
can not find name 'escape'
can not find name 'unescape'

by the way, typescript playground experience is terrible when use Microsoft Edge (input chinese charactors , click a new line , select code)

Bug lib.d.ts help wanted

Most helpful comment

The global escape and unescape are deprecated, please use encodeURI instead. see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape.

If you want to use them, just add the definitions:

declare function escape(s:string): string;

All 14 comments

The global escape and unescape are deprecated, please use encodeURI instead. see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape.

If you want to use them, just add the definitions:

declare function escape(s:string): string;

The global escape and unescape are deprecated, please use encodeURI instead. see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape

@mhegazy It seems nothing refer to that they are deprecated ?

@k8w those pages used to refer to it being deprecated, and it was. It does not appear to be deprecated in the current standard though, and I can't find a specific reference to why the change was made. I suspect they were retained in the specification to not break code, but you shouldn't be using them for new code as they don't handle Unicode URIs properly, and so IMO they are best to leave out and people can add them back in if they are supporting old code.

those pages used to refer to it being deprecated, and it was. It does not appear to be deprecated in the current standard though, and I can't find a specific reference to why the change was made.

That is my recollection as well.. good that i am not crazy :)

I checked with @bterlson, and seems that back then there was a proposal to deprecate Annex B (where all previous mistakes have lived), but then that did not happen. i am assuming that why the deprecation notice was added, then removed.

That said, i do not think we should be in the business of policing ppl's use of APIs. the library should be an accurate model of the JS engine at runtime.. reopening.

Reading the Annex B header

This annex describes various legacy features and other characteristics of web browser based ECMAScript implementations. All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. However, the usage of these features by large numbers of existing web pages means that web browsers must continue to support them. The specifications in this annex define the requirements for interoperable implementations of these legacy features.

These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these features unless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.

It really seems like this should not be in lib.d.ts. People are going to find this, use it instead of a working escape function like encodeURIComponent, and be sad because TypeScript offered up a manifestly broken API.

That is the intention of Annex B, but node supports it as well as all main browsers.. so it is really a standard..

we can have a @deprecated message on it in a comment.

A pragmatic viewpoint is better than one informed by the letter of the standard IMO. I came down on the side of supporting escape/unescape in lib.d.ts because in practice these APIs are supported universally across script engines and used heavily across the web and, despite what Annex B hoped, aren't going anywhere afaict. On the other hand, not having these APIs in lib.d.ts is likely to cause friction and may not prevent people from using it, and may not be effective at driving people toward better APIs.

I also note that we have other things from Annex B in lib.d.ts already, including my best friend String.prototype.blink! I feel like encode/decode should be in too unless we really want to stand on the principle that these APIs are just so bad they should not be used. Annex B shouldn't really factor in to this decision.

Compromise at #19299

We could also split the baby again by making people write window.escape 🤷‍♂️

My opinion is that it is a good compromise with #19299. Splitting the baby again with window.escape feels a bit anti the spirit. For greenfield development, people will (should) be none the wiser of AnnexB. Those supporting legacy code or wanting 👣 🔫 can do so with a little bit of work. window.escape might appropriately _annoy_ those proactively using 👣 🔫 but the poor souls trying to support there legacy code bases without a huge amount of friction would be disserviced.

@mhegazy @RyanCavanaugh escape / unescape are useful exactly because they don't support UTF-8. decodeURIComponent(escape(s)) and unescape(encodeURIComponent(s)) are, even if imperfect, very common and established "patterns" to decode / encode UTF-8 across all browsers including pretty ancient ones.

escape / unescape are useful exactly because they don't support UTF-8.

~Apart from older browser support, can you explain why that makes it useful? 😄~

Actually, I think I see what you mean; you're talking about 8-bit codes?

@DanielRosenwasser Now I'm not sure what you mean, but yeah, it's possible we're talking about the same thing 😄

In this repo, https://github.com/jecovier/vue-json-excel/blob/master/JsonExcel.vue#L175. To achieve feeding btoa with data contains special characters, it is necessary to use unescape(encodeURIComponent(s)) first.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmaurer picture wmaurer  ·  3Comments

Zlatkovsky picture Zlatkovsky  ·  3Comments

uber5001 picture uber5001  ·  3Comments

blendsdk picture blendsdk  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments