|
| 1 | +# Typescript strict mode plugin |
| 2 | + |
| 3 | +Typescript plugin that allows turning on strict mode in specific files or directories. |
| 4 | + |
| 5 | +## Do i need this plugin? |
| 6 | +This plugin was created for bigger repositories that want to incorporate typescript strict mode, but project is so big that refactoring everything would take ages. This plugin allows user to simply put `//@ts-strict` comment to a top of a file and turn a strict mode to that file. If needed, strict mode can be turned on to directories too. |
| 7 | + |
| 8 | +## How to install |
| 9 | + |
| 10 | + Use `npm`: |
| 11 | +```bash |
| 12 | +npm i --save-dev typescript-strict-plugin |
| 13 | +``` |
| 14 | +or yarn |
| 15 | +```bash |
| 16 | +yarn add -D typescript-strict-plugin |
| 17 | +``` |
| 18 | +and add plugin to your `tsconfig.json`: |
| 19 | +```json |
| 20 | +{ |
| 21 | + "compilerOptions": { |
| 22 | + ... |
| 23 | + "strict": false, |
| 24 | + "plugins": [ |
| 25 | + { |
| 26 | + "name": "typescript-strict-plugin" |
| 27 | + } |
| 28 | + ] |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | +That's it! You should be able to use `@ts-strict` comment to strictly check your files. |
| 33 | + |
| 34 | +## Configuration |
| 35 | +Plugin takes one extra non mandatory argument `paths` that is an array of relative or absolute paths of directories that should be included. |
| 36 | +```json |
| 37 | +{ |
| 38 | + "compilerOptions": { |
| 39 | + ... |
| 40 | + "strict": false, |
| 41 | + "plugins": [ |
| 42 | + { |
| 43 | + "name": "typescript-strict-plugin", |
| 44 | + "paths": [ |
| 45 | + "./src", |
| 46 | + "/absolute/path/to/source/" |
| 47 | + ] |
| 48 | + } |
| 49 | + ] |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | +All files contained in those paths will be be strictly checked. Yay! |
| 54 | + |
| 55 | +## Examples |
| 56 | +Let's consider this type and a variable |
| 57 | +```typescript |
| 58 | +interface TestType { |
| 59 | + bar: string; |
| 60 | +} |
| 61 | + |
| 62 | +const foo: TestType | undefined = undefined; |
| 63 | +``` |
| 64 | +1. No `paths` argument |
| 65 | +With `tsconfig.json` like this: |
| 66 | +```json |
| 67 | +{ |
| 68 | + "compilerOptions": { |
| 69 | + ... |
| 70 | + "strict": false, |
| 71 | + "plugins": [ |
| 72 | + { |
| 73 | + "name": "typescript-strict-plugin" |
| 74 | + } |
| 75 | + ] |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | +Typescript will produce errors: |
| 80 | +```typescript |
| 81 | +//@ts-strict |
| 82 | +... |
| 83 | +const boo = foo.bar; // TS2532: Object is possibly 'undefined'. |
| 84 | +``` |
| 85 | +Or not, depending on whether we used `ts-strict` or not: |
| 86 | +```typescript |
| 87 | +//no strict comment here |
| 88 | +... |
| 89 | +const boo = foo.bar; // no error here |
| 90 | +``` |
| 91 | + |
| 92 | +2. With `paths` argument |
| 93 | + With `tsconfig.json` like this: |
| 94 | +```json |
| 95 | +{ |
| 96 | + "compilerOptions": { |
| 97 | + ... |
| 98 | + "strict": false, |
| 99 | + "plugins": [ |
| 100 | + { |
| 101 | + "name": "typescript-strict-plugin", |
| 102 | + "path": "./src" |
| 103 | + } |
| 104 | + ] |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | +If file is in the directory typescript will produce errors even if `ts-strict` comment is not in the file : |
| 109 | +```typescript |
| 110 | +// ./src/index.ts |
| 111 | +const boo = foo.bar; // TS2532: Object is possibly 'undefined'. |
| 112 | +``` |
| 113 | +If file is not in the diretory there will be no error |
| 114 | +```typescript |
| 115 | +// ./lib/index.ts |
| 116 | +const boo = foo.bar; // no error here |
| 117 | +``` |
| 118 | +If file is not in the diretory but there is `ts-strict` file will be check with strict mode: |
| 119 | +```typescript |
| 120 | +// ./lib/index.ts |
| 121 | +//@ts-strict |
| 122 | +... |
| 123 | +const boo = foo.bar; // TS2532: Object is possibly 'undefined'. |
| 124 | +``` |
| 125 | + |
| 126 | +## Testing the plugin |
| 127 | +### Manually |
| 128 | +run |
| 129 | +```bash |
| 130 | +npm i |
| 131 | +``` |
| 132 | +inside root folder and `sample-project` folder and then run |
| 133 | +```bash |
| 134 | +npm run build |
| 135 | +``` |
| 136 | +or |
| 137 | +```bash |
| 138 | +npm run dev |
| 139 | +``` |
| 140 | +and restart typescript service inside `sample-project`. Files in `sample-project` folder should use a local plugin. |
| 141 | +After you made changes to a plugin you should probably restart typescript service in order to reload the plugin. |
| 142 | + |
| 143 | +### E2E tests |
| 144 | +In order to run e2e tests run |
| 145 | + |
| 146 | +```bash |
| 147 | +npm run build && npm run e2e |
| 148 | +``` |
| 149 | + |
| 150 | +## Contributing |
| 151 | +Feel free to create PR's and issues. |
0 commit comments