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.

Typescript incorrect types

See original GitHub issue

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.68.1
  • OS Version: Windows_NT x64 10.0.19044

Steps to Reproduce:

  1. Open VSCode and create a new text file (Ctrl+N)
  2. Set the language to Typescript
  3. Paste this code into it:
export interface Foo {
  objs: { id(): string; }[];
}

export class Test {
  private foo: Foo | null = null;

  doSomething(ids: Record<string, boolean>) {
    if (!this.foo) return;
  
    for (let i = 0; i < this.foo.objs.length; i++) {
      const obj = this.foo.objs[i];
      const objId = obj.id();

      if (!ids[objId]) return;
    }
  }
}
  1. The type of obj on line 12 is incorrectly identified as any rather than { id(): string; }. This can cause errors in a project with noImplicitAny set to true
  2. The Typescript compiler compiles the code without issue, so I believe this is a bug only with the intellisense.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Jul 7, 2022

The circularity is this:

  • What is the type of obj ? It’s determined by its initializer, what is its type?
  • The initializer expression is this.foo.objs[0]. What is its type?
  • That depends on whether this.foo is initialized or not. Is it?
  • That depends on whether it’s possible to enter this loop body a second time, since the answer to this question controls whether or not the rest of the loop body needs to checked to see if this.foo changes type. Is it possible to enter the loop a second time?
  • That depends on ids[objId]. Is this always truthy?
  • That depends on whether objId is a statically-knowable index. Is it?
  • That depends on its initializer, which is obj.id. What’s the type of that?
  • That depends on the type of obj. What is the type of obj? <-- ♻ this is the question being asked
0reactions
fatcerberuscommented, Jul 8, 2022

This is just an educated guess, but I’m assuming that obtaining objId from a function short-circuits the check here:

That depends on whether objId is a statically-knowable index. Is it? [No, because it came from a function call.]

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to overwrite incorrect TypeScript type definition installed ...
Say that I want to use dotenv module in my TypeScript project and install its .d.ts using npm install @types/dotenv --save . Then...
Read more >
TypeScript Done Wrong - OpenReplay Blog
TypeScript Done Wrong · Overusing the any type · Using the Function type instead of specifying the signature · Open Source Session Replay...
Read more >
Documentation - Do's and Don'ts - TypeScript
❌ Don't ever use the types Number , String , Boolean , Symbol , or Object These types refer to non-primitive boxed objects...
Read more >
Typescript imports are incorrectly imported from `@types/`
What steps will reproduce the problem? Use jsx in a tsx file. Get the prompt to import react. Import react; WS inserts import...
Read more >
Surviving the TypeScript Ecosystem — Part 4 - Medium
Surviving the TypeScript Ecosystem — Part 4: Working with Types and Type ... Now we can remove our incorrect module definition for lodash...
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