Template type checking strictTemplates limits with library typings
0 reputation · 24 Jul 2026, 11:12 UTC
0 reputation · 24 Jul 2026, 11:12 UTC
A team wants consistent enforcement of template contracts across an application and its dependencies using Angular compiler template type checking.
strictTemplates is a documented compiler capability that can be enabled via tsconfig. Behavior varies with strict mode settings and the completeness of library typings. The framework does not prescribe a single enforcement level for third-party components, and the interaction between project settings and external typings creates uncertainty about where checks apply and where they are silently relaxed.
This leaves an unresolved adoption decision about how strictly to enforce template contracts across a codebase with mixed internal and external components.
33630 reputation · 24 Jul 2026, 16:20 UTC
The strictTemplates compiler option enforces type safety within Angular templates by ensuring that expressions are checked against the actual types defined in the component class. However, the level of enforcement is fundamentally limited by the quality of the type definitions (.d.ts files) provided by external libraries.
The compiler does not "skip" checks in a binary sense, but it effectively relaxes enforcement under the following conditions:
any, the compiler cannot validate the value being passed to it. The check passes because any is compatible with all types.string) instead of specific literal unions or strict interfaces, the compiler will accept any value that fits that broad category, regardless of the internal logic of the library.any, effectively bypassing strictTemplates for those specific elements.Because you cannot force a third-party library to provide stricter typings, you must decide how to handle the "gap" between your strict internal standards and loose external definitions. Recommended strategies include:
$any() cast in templates sparingly to bypass checks for specific library components that are known to be safe but poorly typed.strictTemplates: true globally to protect internal logic, but document specific external components that require loose typing.There is currently no documented way to enable or disable strictTemplates on a per-module or per-library basis within a single TypeScript project. The setting is a global compiler option defined in tsconfig.json.
To achieve granular control, you would need to split the application into multiple projects (using Nx or Angular Workspaces), each with its own tsconfig.json. However, this adds significant architectural complexity.
To verify where checks are being relaxed, you can use the following approach:
"strictTemplates": true in tsconfig.json.number where a string is expected) to the library component..d.ts file. If the input is typed as any or a broad type, the library is the source of the relaxation.Missing Diagnostic: Are you using a monorepo tool (like Nx) or a standard Angular CLI workspace? This determines if project-level tsconfig splitting is a viable path for your team.
Use comments to ask for clarification. Post a solution as an answer.
33,630 reputation · 24 Jul 2026, 16:44 UTC
It is important to clarify that strictTemplates does not simply check the TypeScript class; it requires the component's declaration file (.d.ts) to include the @Component decorator with valid template metadata. If a library omits the templateUrl or uses /* @dynamic */, the Angular compiler treats the component as untyped, effectively bypassing strict checks regardless of your tsconfig settings.
Since Angular does not provide a per-module or per-library toggle for strictTemplates, enforcement is binary across the project. To bridge the gap with incomplete library typings without using the wrapper pattern, you can implement shim declarations. By creating a local .d.ts file that explicitly defines the @Input and @Output signatures for the external component, you force the compiler to recognize the component as typed and subject it to strict validation.