12

I'm studying Javascript using Visual Code and every time a similar exercise that uses 'event' (the event shows in the code with the strikethrough like e̶v̶e̶n̶t̶) appears I can't complete it because of this annoying issue. In the description pop up a warning showing the issue ("event is deprecated ts(6385)"). I look out in the forums and stack over flow but I can not find any answer for this problem, only a few places says the lib dom and @deprecated, but I don't what to do.

Please, any way to help and learn to pass this problem out will be very useful.

function sayMyFirstName(element){
    alert("My First name is..." + element.value)

}

function sayMyLastName(){
    console.log(event)

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AllesonCoelho
  • 131
  • 1
  • 1
  • 4
  • 1
    Q: Out of curiosity: are you getting `event is deprecated ts(6385)` at the line you showed, "console.log(event)"? Is there other, relevant code you're *NOT* showing us? Where is "event" declared? Is the file suffix "js"? Or ".ts"? In any case - read my suggestions below. – paulsm4 Oct 29 '20 at 04:52
  • @paulsm4 Is js. In my source code material where i am studying they say that "event is global and you can call him from whatever the reason." The code I showed above it's just an example. I'm think the problem is with gitignore, and I paste the /*tslint:disabled*/ in my files. The problem comes and goes, the tricks you said worked in one problem but not in other, I need to sleep and I'll say to you tomorrow. – AllesonCoelho Oct 29 '20 at 05:20
  • If you need more info, look here: https://github.com/microsoft/TypeScript/issues/32533 and here: https://developer.mozilla.org/en-US/docs/Web/API/Window/event. See my additional comments below. – paulsm4 Oct 29 '20 at 05:54
  • Q: Is your question resolved? Did the additional info help? If your source materials tell you to use the global event object - it's probably time to look for updated source materials ;) – paulsm4 Oct 30 '20 at 01:42

4 Answers4

12

It sounds like you're getting TypeScript validation for a simple JS project. There are several things you can try:

  • In your settings file (settings.json):

    "typescript.validate.enable": false

... OR ...

  • In your .js source file(s):

    /*tslint:disabled*/


A separate issue is why you're getting the "deprecation" warning in the first place. This is the reason:

https://developer.mozilla.org/en-US/docs/Web/API/Window/event

The read-only Window property event returns the Event which is currently being handled by the site's code. Outside the context of an event handler, the value is always undefined.

You should avoid using this property in new code, and should instead use the Event passed into the event handler function. This property is not universally supported and even when supported introduces potential fragility to your code.

In other words, "event" should really be passed as an argument to a JS event handler. You shouldn't be using the global object; you shouldn't NEED to use the global object.

Here are a few good tutorials:

Strong suggestion:

If you're learning JavaScript, please make sure your study materials are up-to-date (definitely covering ES6!). This is a good book: Secrets of the JavaScript Ninja 2nd Edition

jasonleonhard
  • 12,047
  • 89
  • 66
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1
    The first trick worked! Thank you. This really help me. I tried to upvote you, but i cant do this right now. – AllesonCoelho Oct 29 '20 at 04:57
  • Interestingly, it isn't working for me. Most of my projects are TS so I only turned validation off for the "workspace" of this one particular PHP/JS/jQuery project I work on. I'm going to play with adding the folder to a workspace and see if that makes a difference. And search for more answers. Might be back with a new question though. – tim.rohrer Nov 02 '20 at 00:32
  • Now it worked. For those working with different types of projects, you may need to change your settings for just the Workspace, and then add your non-TS project to that Workspace. – tim.rohrer Nov 02 '20 at 00:40
  • 1
    @tim.rohrer: in general, "Warnings are Good". Don't forget: the whole reason for inventing TS in the first place is to ensure better "Type safety". So the first priority should always be to *heed* warnings - and correct the underlying problem - whenever possible. "Gagging warnings" should be considered a "last resort". In this case, it sounds like the OP was using some outdated tutorial that used the global "window.event" object; a practice which is now strongly *discouraged*. I hope the OP heeds the warning ... and finds some new tutorials :) – paulsm4 Nov 02 '20 at 02:47
  • @paulsm4 I understand that TS warnings in a TS project make sense. In this case, a TS warning was being thrown for a jQuery call (`onClick`), claiming the method was deprecated when it isn't. I saw nothing indicating there was any validity. – tim.rohrer Nov 02 '20 at 15:06
  • 1
    Maybe the real topic to be discussed at this stage is why would VS Code's TS validator algorithm trigger in a javascript file (`.js`) without any Typescript in the project and without Typescript installed? – tim.rohrer Nov 02 '20 at 15:08
4

You can use "window.event" to replace the deprecated "event".

  • The above perfectly replaces `d3.pointer(event, this)` with `d3.pointer(window.event, this)` in `d3.js` – smpa01 May 20 '22 at 16:02
2

It looks to me that the TypeScript Validator is saying the implicit passing of event is deprecated. A simple fix would be to pass the event into the function as a parameter

function sayMyLastName(event){
    console.log(event)
}

I'm migrating a lot of functionality from JS to Angular and found this issue a lot

ConorJohn
  • 637
  • 2
  • 11
  • 24
0

You just need to open >Preferences: Open Workspace Settings (JSON) by Ctrl + Shift + P in VSCode then add this line to JSON file "editor.showDeprecated": false to disable a show deprecated in case if you only need to some workspace.

If you want to disable all workspace then use >Preferences: Open Settings (JSON) instead.

nakorndev
  • 803
  • 2
  • 11
  • 18