GithubHelp home page GithubHelp logo

classicvalues / eslint-plugin-no-explicit-type-exports Goto Github PK

View Code? Open in Web Editor NEW

This project forked from intuit/eslint-plugin-no-explicit-type-exports

0.0 1.0 0.0 797 KB

A plugin to guard against exporting imported types.

License: MIT License

JavaScript 1.57% TypeScript 98.43%

eslint-plugin-no-explicit-type-exports's Introduction

eslint-plugin-no-explicit-type-exports

This plugin guards against attempting to export types that may not exist after type extraction. This usually occurs when you have a file that exports an imported type.

All Contributors

Example Usage

Imagine a you have a typescript package that includes foo.ts and index.ts:

// foo.ts
export type barType = string;

// index.ts
export { barType } from './foo.ts';

When foo.ts is built into JavaScript, the types are removed and added to a type declaration file. However, the export statement in index.ts will not be removed. When another developer tries to use this package in a JavaScript project they can encounter errors that the export doesn't actually exist.

 ./node_modules/some_repo/index.js
 attempted import error: 'barType' is not exported from './foo'.

Installation

yarn add --dev eslint-plugin-no-explicit-type-exports

Usage

In your .eslintrc add eslint-plugin-no-explicit-type-exports to the plugin section.

{
  "plugins": ["eslint-plugin-no-explicit-type-exports"],
  "rules": {
    "no-explicit-type-exports/no-explicit-type-exports": 2
  }
}

Note: you will need to use the @typescript-eslint/parser and add an import/resolver to your eslint settings. More information about import/resolver can be found in the eslints-plugin-import documentation. Links to both @typescript-eslint and eslints-plugin-import can be found at the bottom of this README.

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json",
    "sourceType": "module"
  },
  "plugins": ["@typescript-eslint"],
    "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }

Rule Details

given:

// foo.ts
export type aType = string;

export randomNumber = 5;

export interface anInterface {
    bar: string;
}

// bar.ts
interface anInterface {
    a: string;
    b: number;
}

type aType = string;

const randomNumber = 5;

export {aType, anInterface};
export default randomNumber;

// baz.ts
type aType = string;

export default aType;

// oneLine.ts
export type x = keyof typeof String;

Valid:

import { aType, randomNumber, anInterface } from './foo';
import {aType, randomNumber, anInterface} from './foo';

export randomNumber;
// exporting * is valid. Since types and interfaces are already stript out
import { aType, randomNumber, anInterface } from './foo';

export * from './foo';
import {aType} from './foo';

export type {aType};

Invalid:

import {aType, randomNumber, anInterface} from './foo';

export anInterface;
import {aType, randomNumber, anInterface} from './foo';

export aType;
export { aType } from './baz';
export { x } from './oneLine';

Fixable

The --fix option will add the 'type' operator to all types and interfaces that are imported then exported from a file.

This:

import { SomeThing } from './some-module.js';
export { SomeThing };

Will be fixed to:

import type { SomeThing } from './some-module.js';
export type { SomeThing };

Further Reading

This plugin uses the eslints-plugin-import resolver to resolve the files that I would be accessing to check if the specifier was a type or an interface. https://github.com/benmosher/eslint-plugin-import

It also uses the @typescript-eslint/parse to parse files to check for types.

https://github.com/typescript-eslint/typescript-eslint

Contributing

Contributions are welcome! If you encounter problems or have a feature suggestion we'd love to hear about it. Open an issue in the GitHub issue tracker and we will do our best to provide support. Thank you!

Contributors โœจ

Thanks goes to these wonderful people (emoji key):


Kendall Gassner

๐Ÿ’ป ๐Ÿ“– ๐Ÿค” ๐Ÿš‡

Andrew Lisowski

๐Ÿ“–

Rashmi K A

โš ๏ธ

Rajat Biswas

๐Ÿ’ป

alexogeny

โš ๏ธ ๐Ÿ’ป

Diana Toader

๐Ÿ“– โš ๏ธ ๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

eslint-plugin-no-explicit-type-exports's People

Contributors

alexogeny avatar classicvalues avatar dependabot[bot] avatar dianatdr avatar hipstersmoothie avatar kendallgassner avatar mend-bolt-for-github[bot] avatar rajatdiptabiswas avatar stabbylambda avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.