diff --git a/docs/generated/packages/nest/generators/application.json b/docs/generated/packages/nest/generators/application.json index 6350158831da0..b14777ae8e8e0 100644 --- a/docs/generated/packages/nest/generators/application.json +++ b/docs/generated/packages/nest/generators/application.json @@ -68,6 +68,11 @@ "type": "boolean", "description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.", "default": false + }, + "strict": { + "type": "boolean", + "description": "Adds strictNullChecks, noImplicitAny, strictBindCallApply, forceConsistentCasingInFileNames and noFallthroughCasesInSwitch to tsconfig.", + "default": false } }, "additionalProperties": false, diff --git a/docs/generated/packages/nest/generators/library.json b/docs/generated/packages/nest/generators/library.json index 2fe5b804d4fa7..6493e41676182 100644 --- a/docs/generated/packages/nest/generators/library.json +++ b/docs/generated/packages/nest/generators/library.json @@ -110,7 +110,7 @@ "strict": { "description": "Whether to enable tsconfig strict mode or not.", "type": "boolean", - "default": false + "default": true }, "standaloneConfig": { "description": "Split the project configuration into /project.json rather than including it inside workspace.json", diff --git a/e2e/node/src/node.test.ts b/e2e/node/src/node.test.ts index 98f8230525e9f..35253133bf661 100644 --- a/e2e/node/src/node.test.ts +++ b/e2e/node/src/node.test.ts @@ -511,8 +511,8 @@ describe('nest libraries', function () { `libs/${nestlib}/src/lib/foo.model.ts`, ` export class FooModel { - foo: string; - bar: number; + foo?: string; + bar?: number; }` ); @@ -527,7 +527,7 @@ exports.FooModel = void 0; const openapi = require("@nestjs/swagger"); class FooModel { static _OPENAPI_METADATA_FACTORY() { - return { foo: { required: true, type: () => String }, bar: { required: true, type: () => Number } }; + return { foo: { required: false, type: () => String }, bar: { required: false, type: () => Number } }; } } exports.FooModel = FooModel; diff --git a/packages/nest/src/generators/application/application.spec.ts b/packages/nest/src/generators/application/application.spec.ts index cbd50dc5e1dbb..c3a722bcb7e9f 100644 --- a/packages/nest/src/generators/application/application.spec.ts +++ b/packages/nest/src/generators/application/application.spec.ts @@ -59,6 +59,22 @@ describe('application generator', () => { ]); }); + it('should add strict checks with --strict', async () => { + await applicationGenerator(tree, { name: appName, strict: true }); + const tsConfig = devkit.readJson( + tree, + `apps/${appDirectory}/tsconfig.app.json` + ); + + expect(tsConfig.compilerOptions.strictNullChecks).toBeTruthy(); + expect(tsConfig.compilerOptions.noImplicitAny).toBeTruthy(); + expect(tsConfig.compilerOptions.strictBindCallApply).toBeTruthy(); + expect( + tsConfig.compilerOptions.forceConsistentCasingInFileNames + ).toBeTruthy(); + expect(tsConfig.compilerOptions.noFallthroughCasesInSwitch).toBeTruthy(); + }); + describe('--skipFormat', () => { it('should format files', async () => { jest.spyOn(devkit, 'formatFiles'); diff --git a/packages/nest/src/generators/application/lib/normalize-options.ts b/packages/nest/src/generators/application/lib/normalize-options.ts index dd7dc07483906..4e358a0c2f9f5 100644 --- a/packages/nest/src/generators/application/lib/normalize-options.ts +++ b/packages/nest/src/generators/application/lib/normalize-options.ts @@ -25,6 +25,7 @@ export function normalizeOptions( return { ...options, + strict: options.strict ?? false, appProjectRoot, linter: options.linter ?? Linter.EsLint, unitTestRunner: options.unitTestRunner ?? 'jest', diff --git a/packages/nest/src/generators/application/lib/update-tsconfig.ts b/packages/nest/src/generators/application/lib/update-tsconfig.ts index 748b82b02149c..232f605f6b048 100644 --- a/packages/nest/src/generators/application/lib/update-tsconfig.ts +++ b/packages/nest/src/generators/application/lib/update-tsconfig.ts @@ -9,6 +9,16 @@ export function updateTsConfig(tree: Tree, options: NormalizedOptions): void { (json) => { json.compilerOptions.emitDecoratorMetadata = true; json.compilerOptions.target = 'es2015'; + if (options.strict) { + json.compilerOptions = { + ...json.compilerOptions, + strictNullChecks: true, + noImplicitAny: true, + strictBindCallApply: true, + forceConsistentCasingInFileNames: true, + noFallthroughCasesInSwitch: true, + }; + } return json; } ); diff --git a/packages/nest/src/generators/application/schema.d.ts b/packages/nest/src/generators/application/schema.d.ts index e5b9d3d54b74d..42a2b6ad3bd2e 100644 --- a/packages/nest/src/generators/application/schema.d.ts +++ b/packages/nest/src/generators/application/schema.d.ts @@ -13,6 +13,7 @@ export interface ApplicationGeneratorOptions { e2eTestRunner?: 'jest' | 'none'; setParserOptionsProject?: boolean; rootProject?: boolean; + strict?: boolean; } interface NormalizedOptions extends ApplicationGeneratorOptions { diff --git a/packages/nest/src/generators/application/schema.json b/packages/nest/src/generators/application/schema.json index 3acc26f63f81d..55c69037e4a10 100644 --- a/packages/nest/src/generators/application/schema.json +++ b/packages/nest/src/generators/application/schema.json @@ -68,6 +68,11 @@ "type": "boolean", "description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.", "default": false + }, + "strict": { + "type": "boolean", + "description": "Adds strictNullChecks, noImplicitAny, strictBindCallApply, forceConsistentCasingInFileNames and noFallthroughCasesInSwitch to tsconfig.", + "default": false } }, "additionalProperties": false, diff --git a/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap index a25862919f781..9102dbcae9956 100644 --- a/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap @@ -32,7 +32,13 @@ export default { exports[`lib --unit-test-runner none should not generate test configuration 1`] = ` { "compilerOptions": { + "forceConsistentCasingInFileNames": true, "module": "commonjs", + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noPropertyAccessFromIndexSignature": true, + "strict": true, }, "extends": "../../tsconfig.base.json", "files": [], @@ -62,7 +68,13 @@ exports[`lib --unit-test-runner none should not generate test configuration 2`] exports[`lib nested should create a local tsconfig.json 1`] = ` { "compilerOptions": { + "forceConsistentCasingInFileNames": true, "module": "commonjs", + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noPropertyAccessFromIndexSignature": true, + "strict": true, }, "extends": "../../../tsconfig.base.json", "files": [], @@ -94,7 +106,13 @@ export class MyLibModule {} exports[`lib not nested should create a local tsconfig.json 1`] = ` { "compilerOptions": { + "forceConsistentCasingInFileNames": true, "module": "commonjs", + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noPropertyAccessFromIndexSignature": true, + "strict": true, }, "extends": "../../tsconfig.base.json", "files": [], diff --git a/packages/nest/src/generators/library/lib/normalize-options.ts b/packages/nest/src/generators/library/lib/normalize-options.ts index 98921c54bbbf1..8aaeb0a7c31d7 100644 --- a/packages/nest/src/generators/library/lib/normalize-options.ts +++ b/packages/nest/src/generators/library/lib/normalize-options.ts @@ -28,6 +28,7 @@ export function normalizeOptions( const normalized: NormalizedOptions = { ...options, + strict: options.strict ?? true, controller: options.controller ?? false, fileName, global: options.global ?? false, diff --git a/packages/nest/src/generators/library/lib/update-tsconfig.ts b/packages/nest/src/generators/library/lib/update-tsconfig.ts index 8eaffe7c27267..207531ef14f59 100644 --- a/packages/nest/src/generators/library/lib/update-tsconfig.ts +++ b/packages/nest/src/generators/library/lib/update-tsconfig.ts @@ -10,9 +10,10 @@ export function updateTsConfig(tree: Tree, options: NormalizedOptions): void { if (options.strict) { json.compilerOptions = { ...json.compilerOptions, + strictNullChecks: true, + noImplicitAny: true, + strictBindCallApply: true, forceConsistentCasingInFileNames: true, - strict: true, - noImplicitReturns: true, noFallthroughCasesInSwitch: true, }; } diff --git a/packages/nest/src/generators/library/library.spec.ts b/packages/nest/src/generators/library/library.spec.ts index 10e1c04592326..ecb814062f71e 100644 --- a/packages/nest/src/generators/library/library.spec.ts +++ b/packages/nest/src/generators/library/library.spec.ts @@ -247,35 +247,14 @@ describe('lib', () => { it('should update the projects tsconfig with strict true', async () => { await libraryGenerator(tree, { name: libName, strict: true }); - const tsconfigJson = readJson( - tree, - `/libs/${libFileName}/tsconfig.lib.json` - ); - expect(tsconfigJson.compilerOptions.strict).toBe(true); - expect( - tsconfigJson.compilerOptions.forceConsistentCasingInFileNames - ).toBe(true); - expect(tsconfigJson.compilerOptions.noImplicitReturns).toBe(true); - expect(tsconfigJson.compilerOptions.noFallthroughCasesInSwitch).toBe( - true - ); - }); - - it('should default to strict false', async () => { - await libraryGenerator(tree, { name: libName }); - - const tsconfigJson = readJson( - tree, - `/libs/${libFileName}/tsconfig.lib.json` - ); - expect(tsconfigJson.compilerOptions.strict).not.toBeDefined(); - expect( - tsconfigJson.compilerOptions.forceConsistentCasingInFileNames - ).not.toBeDefined(); - expect(tsconfigJson.compilerOptions.noImplicitReturns).not.toBeDefined(); + const tsConfig = readJson(tree, `/libs/${libFileName}/tsconfig.lib.json`); + expect(tsConfig.compilerOptions.strictNullChecks).toBeTruthy(); + expect(tsConfig.compilerOptions.noImplicitAny).toBeTruthy(); + expect(tsConfig.compilerOptions.strictBindCallApply).toBeTruthy(); expect( - tsconfigJson.compilerOptions.noFallthroughCasesInSwitch - ).not.toBeDefined(); + tsConfig.compilerOptions.forceConsistentCasingInFileNames + ).toBeTruthy(); + expect(tsConfig.compilerOptions.noFallthroughCasesInSwitch).toBeTruthy(); }); }); diff --git a/packages/nest/src/generators/library/schema.json b/packages/nest/src/generators/library/schema.json index df3e8967e2e9c..2f96990c52b63 100644 --- a/packages/nest/src/generators/library/schema.json +++ b/packages/nest/src/generators/library/schema.json @@ -110,7 +110,7 @@ "strict": { "description": "Whether to enable tsconfig strict mode or not.", "type": "boolean", - "default": false + "default": true }, "standaloneConfig": { "description": "Split the project configuration into /project.json rather than including it inside workspace.json",