A guide on converting your current Angular app into a "fully strict" application – follow these steps!

I started developing an Angular Application back in Angular 4 and now it has been upgraded to Angular 12. However, during the initial development phase, the strict mode was not enabled. Now that the application is stable and live in production, I am looking for the best way to gradually enable strict mode instead of turning on all strict options at once.

I came across a helpful answer on enabling strict mode in Angular 10 here, which gives a general overview of enabling strict mode.

My specific question is about how to enable strict mode in stages. Considering the medium size and complexity of the application, testing all the changes at once would be a significant challenge. There are various options in Angular strict config such as some that can be manually configured and some that are turned on by strict:true:

  1. noImplicitAny
  2. strictInjectionParameters
  3. strictTemplates
  4. strictNullChecks
  5. Enable in tslint.json by setting "no-any": true

What is the most effective way to enable these settings gradually, in a specific order, to ensure thorough testing before deploying to production? I would appreciate any guidance, especially from someone who has successfully updated an existing project incrementally rather than all at once.

Answer №1

Here is a helpful strategy:

  1. Start by defining all your models - interfaces/types/classes - as the ultimate source of truth for types. Avoid using the any type whenever possible during this process.
  2. Ensure to type your functions - While the language service/compiler might infer types for functions, explicitly adding types will notify you of any incorrect function usage.
  3. Take advantage of Generics - Angular offers functions that accept generic types to ensure the expected type. For instance, when expecting a number from an api call, specify that type in the function invocation like so:
    this.http.get<number>(someAPIUrl)
    . Libraries usually have detailed documentation on typings.
  4. Enable tsconfig flags like noImplicitAny to pinpoint areas needing more typing that may have been overlooked. You can also consider enabling strict mode.
  5. Activate template type checking - Refer to Angular docs for guidance. It's incredibly beneficial to navigate from a template object property directly to its class/interface/type definition.

Be prepared for some trial and error as you work towards your goal. Expect some things to break before they get fixed. Take it one step at a time.

Notably, the Angular Team continues to enhance typing with initiatives like Typed Forms.

Answer №2

Currently, I am in the process of developing an application that has made the transition from Angular 1 to Angular 12. Working within a team environment on a project of medium to large size, utilizing incremental tooling has proven to be essential in ensuring that we are on the right path.

  1. Implement eslint rules such as explicit-module-boundary-types.
  2. Enable
    angularCompilerOptions.strictTemplates
    in your tsconfig.json file (or a subset of it).
  3. Develop a tsconfig.strict.json file that compiles a portion of your application using the files and includes options. Then, incorporate
    tsc -project src/tsconfig.strict.json --noEmit --incremental
    into our automated testing process. This will ensure that new files are added and that any important existing files are accounted for.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering difficulty invoking a component method from d3's call() function

My current setup involves using D3 to drag and drop links in the following manner: .call(d3.drag() .on("start", linkDragStart) .on("drag", linkDragging) .on("end", linkDragEnd)); Recently, I decided to extract this functionality into a separate met ...

The property you are trying to access is not found within the declared type of 'IntrinsicAttributes & { children?: ReactNode; }'

In my React project created using Create-React-App, I have included the following packages (relevant to the issue at hand): "react": "^16.13.1", "react-dom": "^16.13.1", "react-router-dom": "^5.1.2", "react-scripts": "3.4.1", "typescript": "^3.9.2", "@typ ...

The date format being sent by Angular to Java is incorrect, resulting in the inability to create an object in the

In my coupon system, I am experiencing an issue with the date format. The Java coupon bean has a date.sql startDate and endDate, while the Angular coupon model includes startDate:Date and endDate:Date in its constructor. However, when I display the dates o ...

What is the most effective way to split time into two separate parts?

Suppose a user enters the time as 12:34 and we need to split it into two different parts to save it in an array like [12, 34]. How can this be achieved using Angular? I attempted to split them but my solutions were unsuccessful! I am currently utilizing & ...

Delay in Updating Nativescript Slider Value

After developing a metronome using the Nativescript Slider here to adjust speed (interval), I encountered an issue with incorrect updating of values. The code initially worked well, allowing real-time speed changes: app.component.html <Slider #sl min ...

Content obscuring dropdown menu

I am currently working on a screen design that resembles the following: return ( <SafeAreaView> <View style={styles.container}> <View style={styles.topContainer}> <View style={styles.searchFieldContainer}> ...

What is the reason for the return of undefined with getElementsByClassName() in puppeteer?

Currently, I am utilizing puppeteer to fetch certain elements from a webpage, specifically class items (divs). Although I understand that getElementsByClassName returns a list that needs to be looped through, the function always returns undefined for me, e ...

How can I integrate a timer into an Angular carousel feature?

I have put together a carousel based on a tutorial I found on a website. Check out the HTML code for my carousel below: <div class="row carousel" (mouseover)="mouseCheck()"> <!-- For the prev control button ...

Using TypeScript to Verify the Existence of Words in a String

Is there a way in typescript to find specific words within a given string? For example: If we have a list: ['Mr', 'Mrs', 'FM.', 'Sir'] and a string named 'Sir FM. Sam Manekshaw'. The words 'Sir' ...

Issue arising with data exchange between components using data service in Angular 5

Utilizing data service to share information between components has presented a challenge for me. References: Angular: Updating UI from child component to parent component Methods for Sharing Data Between Angular Components Despite attempting the logic o ...

Ways to initiate an HTTP request within switchMap upon emission of a BehaviorSubject value

As I delve into writing angular applications in a declarative style, I find myself pondering on the most effective approach for handling POST requests. Specifically, I am facing a dilemma with regards to calling these requests when dealing with a login for ...

Understanding Multiple Type Scenarios in React with Typescript

Code Demonstration: type PropsType = {top: number} | {bottom: number} // The function that moves something in one direction by a specific distance. function move(props: PropsType) { ... } Expected Usage: move({top: 100}) or move({bottom: 100}) Avoid us ...

What distinguishes ES6 from ES2015 in the TypeScript compiler option `--libs`?

Can you explain the distinction between ES6 and ES2015 in the TypeScript compiler option here? Also, what does --libs do? https://i.sstatic.net/iUv8t.png ...

I'm encountering an issue where the 'switchMap' property is not recognized on the 'Observable<User>' type

I encountered an issue while attempting to run code in git bash. The problem arises from 'switchMap' not being executed and displaying the error message: "error TS2339: Property 'switchMap' does not exist on type 'Observable'" ...

What is the reason behind the error Generic indexed type in Typescript?

Here is a scenario where I have a specific generic type: type MapToFunctions<T> = { [K in keyof T]?: (x: T[K]) => void; }; It functions correctly in this instance: type T1 = { a: string }; const fnmap1: MapToFunctions<T1> = { a: (x: st ...

Changing a particular field value within an array of objects in Angular 4

I have a scenario where I created a class called security: class security { public id:number; public name:string; } Next, I initialized an array of security objects: let s:security[]=[{id:1,name:'Alex'},{id:2,name:'John'},{id:3,nam ...

The Angular translation service may encounter issues when used on different routes within the application

I'm currently working on an Angular application that has multi-language support. To better organize my project, I decided to separate the admin routes from the app.module.ts file and place them in a separate file. However, after doing this, I encounte ...

Is OnPush Change Detection failing to detect state changes?

Curious about the issue with the OnPush change detection strategy not functioning properly in this demonstration. My understanding is that OnPush change detection should activate when a property reference changes. To ensure this, a new array must be set e ...

Uploading files in Angular application

I'm facing some minor issues with file uploads for the first time. My project consists of an Angular 7 app and a NodeJS (express) backend. I have successfully uploaded images through the Angular page and stored them with the correct format in NodeJS. ...

NPM displaying an error message

npm ERROR! Windows_NT 6.3.9600 npm ERROR! Command "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\np ...