npm run huequica:dev

なんか長めのはなしとか

`.eslintrc` や `.prettierrc`, `tsconfig.json` の Interface がどこに存在するのか

たぶんそんなに需要がある話ではないだろうけど。たまたま触る機会があったので。

.prettierrc

Options の名前で export されているのでこれを引っ張って来ればいい。

github.com

import { Options } from 'prettier';

const option: Options = {
  semi: true,
  singleQuote: true,
  tabWidth: 2,
  bracketSpacing: true,
  arrowParens: 'always',
  endOfLine: 'lf',
};

ちなみにこの型定義は prettier をインストールするだけで参照できるようになる。Prettier 側が自動で入れるように指定しているのか、はたまた何かがフックされているのかは分からん。

.eslintrc

こちらは module ではなく Linter という namespace 内部で定義されている。

github.com

import { Linter } from 'eslint';

type BaseConfig = Linter.Config;
type RuleRecord = Linter.RuleRecord;

const config: BaseConfig = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  // 以下省略
}

const addRule = (rule: RuleRecord): void => {
  config.rules = {
    ...config.rules,
    ...rule,
  };
);

多少雑なコードを書いたがこんなイメージだろうか こちらは @types/eslint が必ず必要なので別でインストールが必要

tsconfig.json

一番探すのに苦労した。最終的には pkg-types というモジュールが提供していたのでこちらを使うことになるのかなという気持ちだった。 ちなみに tsconfig.json の interface 以外にも package.json の型定義も提供しており、それのアクセスメソッドや生成メソッドも提供している中々に有益そうなパッケージとなっている。

github.com

www.npmjs.com

tsconfig の型定義は TSConfig という名前で export されているので、以下コードで引っ張ってきて使うことができる

import { TSConfig } from 'pkg-types';

const config: TSConfig = {
  compilerOptions: {
    module: 'commonjs',
    target: 'ES2018',
    // 以下省略
  },
};