microsoft / microsoft/TypeScript

Always verify import path

Open
#40,450 2 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

In many projects is necessary work with files which isn't a TS/JS file. Some common ones are png, jpg, svg, but when importing this files we get Cannot find module or its corresponding type declarations. error.

Searching how to fix that, we come across many discussions suggesting using the global module declaration, such as:

declare module '*.svg' {
  const content: string
  export default content
}

Links

This approach fix the lint error, but disable TS intellisense.

Example

Folder structure

app
 |
 ├ img
 |  └─ top-view.svg
 |
 └─index.ts

index.ts
68747470733a2f2f692e737461636b2e696d6775722e636f6d2f6d36494a592e706e67

No errors, but the correct path is ../img/top-view.svg

Here is the related issue #40333


Suggestion

Compiler could still returning missing type declarations for not TS/JS files. But always check if import path results in a file and return an error if not (even if file is not an TS/JS).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading related issue #40333 and the examples in this report, then investigate TypeScript's import-path resolution behavior for non-TS/JS files. Done means the proposed missing-file diagnostic behavior is defined and implemented without losing support for declared non-code modules.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.