1

I use VS Code for JavaScript.
Definitions within a file work sometimes, but not others. Does anyone know why they fail?
In this example, VS Code finds the definition for edit easily. There is no occurrence of the word 'something' anywhere else in this file. Completely unique. So, why can't VS Code find its definition?

enter image description here

If I knew why, maybe I could change my code to work around it.

VS Code also can't find any references to these 'lost' properties. Which is very frustrating for tracking down where a function is called etc.

enter image description here

Michael G
  • 458
  • 2
  • 9

2 Answers2

0

You should use this.

const edit = {
  something: 5,
  test() {
    this.something;
  },
};

You must not use an arrow function for test field, or this would point elsewhere but not the edit object.

EDIT: In TypeScript 4.3.5, the latest version of TS at the time of writing, TS is able to detect self-references in object literals, and both edit.something and this.something would work. For the case of working with JavaScript, upgrading VS Code should do the trick.

A TS playground link for reference.

T.D. Stoneheart
  • 811
  • 4
  • 7
  • 1
    Thanks for the advice, but to clarify, my code runs perfectly. The only problem is VS Code can't resolve the definitions. – Michael G Aug 24 '21 at 18:01
  • https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers This question might relate to your concerns. :P – T.D. Stoneheart Aug 24 '21 at 18:06
  • OP is not using `this`. They refer to the constant `edit`, which should have a type definition available. – Bergi Aug 24 '21 at 18:30
  • Thank you, @Bergi . @T.D.Stoneheart , my question is really about VS Code. `edit` should have a type definition. For some reason, its definition is `any`. – Michael G Aug 24 '21 at 19:47
  • I guess TS hasn't been able to detect self references in the object literal declaration yet. – T.D. Stoneheart Aug 25 '21 at 04:02
  • @MichaelG @Bergi Maybe upgrading TypeScript or VS Code would work, as TS could determine the type when referring to both `this` and `edit`, at least at TS 3.3.3 in the TS Playground. – T.D. Stoneheart Aug 25 '21 at 04:12
  • @T.D.Stoneheart , this is a randomly occurring bug, _not_ a problem with my code or environment. I am looking for clues as to why and when type definitions can fail. For example, if I open this same file on a new machine, the type definition may or may not populate. The behavior can differ over SSH versus over a local connection, and changes to the file may make the bug re-emerge or vanish again. There is no simple answer to my question. I am looking for others who have experienced the same. – Michael G Aug 25 '21 at 12:03
  • "this is a randomly occurring bug", I've witnessed this in my TS env sometimes, and fix it by re-starting the TS Server. I think the most likely cause is a parsing error when you're mid-editing a file which does not clean up during a later re-parse once you've finished editing; VS Code is running the server against the working version and not the saved version of the file so you get hints as you type. – Paul S. Sep 11 '21 at 12:42
0

There is a length limit. IntelliSense will define type :any if an object description is too long.

There is no way around this at all. The :any type will overwrite imported type definitions (e.g., a .d.ts file). Two solutions:

  1. Don't write long object declarations in your code.
  2. Don't use VS Code or IntelliSense.

As for specifics:

The behavior is unpredictable.

  1. If a type definition does not generate, reducing the object description length will not cause it to generate
  2. Reducing an object description length to below the limit and re-launching VS Code will cause it to re-generate.

The length limit is unpredictable.

  1. A function definition is 'longer' than a string, even if they have the same character count.
  2. A nested ('deep') object declaration is longer than a shallow ('flat') object declaration, even if they have the same character count.

In short: IntelliSense breaks irreparably if you try to write long object declarations in a JS file.

Michael G
  • 458
  • 2
  • 9
  • Do you have a reference for this length limit? – Bergi Sep 16 '21 at 13:21
  • Personal research only. If you know of any mention in any documentation anywhere, I would love to add it here. (Am I just the first coder ever to try writing a long object description in VS Code?) – Michael G Sep 16 '21 at 16:45