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.

No errors for TypeScript `import` types

See original GitHub issue

Docs on import types: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#import-types

Reduced test case (all dependencies are latest):

// ./cypress/test.ts

// Expected error, got one
// Unexpected path "../app/helpers/api" imported in restricted zone
import Api from '../app/helpers/api';

// Expected error, but got none
type Api = typeof import('../app/helpers/api');
// .eslintrc.js
const config = {
  extends: ['plugin:import/typescript'],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 6,
    sourceType: 'module',
  },
  plugins: ['import'],
  rules: {
    'import/no-restricted-paths': [
      2,
      {
        basePath: '.',
        zones: [
          {
            target: './cypress',
            from: './app',
          },
        ],
      },
    ],
  },
}

module.exports = config;

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
Baumgaercommented, Jul 21, 2020

I am feeling free to ask for an additional parameter for each zone.

Is it possible to allow typescript special imports for types?

import type {MyType} from "/normally/forbidden/import/in/restricted/area"

It would be nice to disallow imports which stay in compiled javascript but allow imports of type in this way. The parameter for the zone could be named as "allowTypeImports": true

1reaction
fregantecommented, Jan 2, 2022

None of these run any code so they should be allowed:

import "/normally/forbidden/import/in/restricted/area" // if the file is d.ts, but rare
import {MyType} from "/normally/forbidden/import/in/restricted/area"
import {type MyType} from "/normally/forbidden/import/in/restricted/area"
import type {MyType} from "/normally/forbidden/import/in/restricted/area"
type MyType = typeof import("/normally/forbidden/import/in/restricted/area");
Read more comments on GitHub >

github_iconTop Results From Across the Web

Do I need to use the "import type" feature of TypeScript 3.8 if ...
Short answer: Being more explicit by using import type and export type statements seem to yield explicable benefits by safeguarding against ...
Read more >
Documentation - TypeScript 3.8
import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of...
Read more >
No errors for TypeScript `import` types · Issue #1675 - GitHub
Dynamic imports aren't usually checked, because they aren't forced to be static, and it's impossible to reliably statically know the import path ...
Read more >
TypeScript errors and how to fix them
Common Errors​​ Below you find a list of common TypeScript errors along with the buggy code and its fixed version.
Read more >
consistent-type-imports | typescript-eslint
TypeScript allows specifying a type keyword on imports to indicate that the export exists only in the type system, not at runtime.
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