Skip to content

Missing escape/unescape ? #8639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
John0King opened this issue May 17, 2016 · 17 comments
Closed

Missing escape/unescape ? #8639

John0King opened this issue May 17, 2016 · 17 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@John0King
Copy link

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)

@mhegazy
Copy link
Contributor

mhegazy commented May 17, 2016

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;

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label May 17, 2016
@mhegazy mhegazy closed this as completed May 17, 2016
@k8w
Copy link

k8w commented Oct 18, 2017

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 ?

@kitsonk
Copy link
Contributor

kitsonk commented Oct 18, 2017

@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.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 18, 2017

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.

@mhegazy mhegazy reopened this Oct 18, 2017
@mhegazy mhegazy added Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this Bug A bug in TypeScript and removed Question An issue which isn't directly actionable in code labels Oct 18, 2017
@RyanCavanaugh
Copy link
Member

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.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 18, 2017

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.

@bterlson
Copy link
Member

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.

@RyanCavanaugh
Copy link
Member

Compromise at #19299

@RyanCavanaugh
Copy link
Member

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

@kitsonk
Copy link
Contributor

kitsonk commented Oct 19, 2017

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.

@RReverser
Copy link
Contributor

@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.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 29, 2017

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?

@RReverser
Copy link
Contributor

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

@mhegazy mhegazy added this to the Community milestone Jan 4, 2018
@zheeeng
Copy link

zheeeng commented Jan 23, 2018

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.

@Andarist
Copy link
Contributor

Andarist commented Aug 5, 2023

This was already fixed by #19015 , cc @jakebailey

@RyanCavanaugh
Copy link
Member

Makes me sad actually 🥲

@DanielRosenwasser
Copy link
Member

Well they're marked as @deprecated if it makes you feel any better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

10 participants