Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Typescript plugin that allows turning on strict mode in specific files or directories.

License

NotificationsYou must be signed in to change notification settings

allegro/typescript-strict-plugin

Repository files navigation

Typescript plugin that allows turning on strict mode in specific files or directories.

Do I need this plugin?

typescript-strict-plugin was created mainly for existing projects that want to incorporatetypescript strict mode, but project is so big that refactoring everything would take ages.

Our plugin allows adding strict mode to a TypeScript project without fixing all the errors at once.By adding//@ts-strict-ignore comment at the top of a file, its whole content will be removed fromstrict type checking. To ease migrating a project to use this plugin, you can useupdate-strict-comments script, which adds the ignore comment to all files that contain at leastone strict error.

TypeScript plugins don't work at compile-time. They will show errors in your IDE, but they won'tappear during compilation. To check strict errors in marked files you can usetsc-strict script.This command line tool is created to check for files that should be checked with strict rules incompilation time. It finds all relevant files and checks for strict typescript errors only for thatfiles. Therefore, we have strict errors inside our files and during build time.

How to install

Usenpm:

npm i --save-dev typescript-strict-plugin

or yarn

yarn add -D typescript-strict-plugin

add plugin to yourtsconfig.json:

{"compilerOptions": {..."strict":false,"plugins": [    {"name":"typescript-strict-plugin"    }   ] }}

and run the migration script

./node_modules/.bin/update-strict-comments

That's it! You should be able to see strict typechecking in files without the@ts-strict-ignorecomment. To make these files strict too, just remove its' ignore comments.

Configuration

Plugin takes extra, non-mandatory argumentspaths,exclude andexcludePattern. Argspaths andexclude accept an array of relative or absolute paths that should be included (propertypaths)or excluded (propertyexclude). ArgexcludePattern accepts an array of strings that will bematched withminimatch. To add strict mode to files fromignored paths you can insert//@ts-strict comment.

{"compilerOptions": {..."strict":false,"plugins": [      {"name":"typescript-strict-plugin","paths": ["./src","/absolute/path/to/source/"        ],"exclude": ["./src/tests","./src/fileToExclude.ts"        ],"excludePattern": ["**/*.spec.ts"        ]      }    ]  }}

All files contained in those paths will be strictly checked. Yay!

To add cli tool to your build time you can add a script to scripts list in package.json

{"scripts": {...,"typecheck":"tsc && tsc-strict",  },}

Then you can simply run

yarn tsc-strict

All your strict files should be checked from command line.

You can also pass sometsc arguments to thetsc-strict to override default compiler options e.g.

yarn tsc-strict --strictNullChecksfalse

would not check for the strict null check in your files. Thetsc-strict accepts all the argumentsthat regulartsc command accepts.

Migrating to v2

Because of difficulties with migrating large projects to strict mode with original//@ts-strictcomment, we've taken an another approach. Now in version 2.0+ typescript files are strict bydefault, and to ignore a file, you can use special//@ts-strict-ignore comment. It allows to havestrict mode in newly created files without remembering about adding strict comment at the top of it.Version 2.0 comes with a new scriptupdate-strict-comments, which detects all files with at leastone strict error and adds the ignore comment to ease the migration. To update from v1 to v2, youjust need to run:

update-strict-comments

VSCode support

VSCode supports this plugin out of the box. However, sometimes it can use its own typescript versioninstead of the project one, resulting in not reading the local tsconfig. If you are using VSCode besure to haveUse workspace version option selected inTypescript: Select Typescript Version...command available in thecommand pallete.

image

Testing the plugin

Manually

run

npm i

inside root folder andsample-project folder and then run

npm run build

or

npm run dev

and restart typescript service insidesample-project. Files insample-project folder should usea local plugin. After you made changes to a plugin you should probably restart typescript service inorder to reload the plugin.

Tests

In order to run tests run

npm runtest

Contributing

Feel free to create PR's and issues.

About

Typescript plugin that allows turning on strict mode in specific files or directories.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors13


[8]ページ先頭

©2009-2025 Movatter.jp