Reputation: 10541
in my Angular app i have a component:
import { MakeService } from './../../services/make.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-vehicle-form',
templateUrl: './vehicle-form.component.html',
styleUrls: ['./vehicle-form.component.css']
})
export class VehicleFormComponent implements OnInit {
makes: any[];
vehicle = {};
constructor(private makeService: MakeService) { }
ngOnInit() {
this.makeService.getMakes().subscribe(makes => { this.makes = makes
console.log("MAKES", this.makes);
});
}
onMakeChange(){
console.log("VEHICLE", this.vehicle);
}
}
but in the "makes" property I have a mistake. I dont know what to do with it...
Upvotes: 1004
Views: 1387656
Reputation: 129
input is not always required, you can declare it as optional using the? syntax.
makes?: any;
OR
makes?: string;
Upvotes: 3
Reputation: 3054
According to microsoft/TypeScript#20075, the pull request that implemented the --strictPropertyInitialization
compiler option:
Strict property initialization checks only apply to properties that are declared with proper identifiers. It does not apply to properties with computed names or properties with names specified as string or numeric literals.
// Compile with --strict class Test { a: number; // Error, not initialized "hello world": number; // No check }
In the type {a: string}
or the value {a: ""}
, the a
is an identifier, whereas in the type {"a": string}
or the value {"a": ""}
, the "a"
is a string literal. So the former receives checking while the latter does not.
This answer is copied from the answer of jcalz in the question An object key of a class isn't initialized or assigned in the constructor. Why does quoting it mute the error?
Upvotes: 0
Reputation: 1303
For angular 17 , i did strict:false
in tsconfig.json
and it worked for me
Upvotes: 7
Reputation: 10768
The error is legitimate and may prevent your app from crashing. You typed makes
as an array but it can also be undefined.
You have 2 options (instead of disabling the typescript's reason for existing...):
1. In your case the best is to type makes
as possibily undefined.
makes?: any[]
// or
makes: any[] | undefined
So the compiler will inform you whenever you try to access to makes
that it could be undefined.
Otherwise, if the // <-- Not ok
lines below were executed before getMakes
finished or if getMakes
failed, your app would crash and a runtime error would be thrown. That's definitely not what you want.
makes[0] // <-- Not ok
makes.map(...) // <-- Not ok
if (makes) makes[0] // <-- Ok
makes?.[0] // <-- Ok
(makes ?? []).map(...) // <-- Ok
2. You can assume that it will never fail and that you will never try to access it before initialization by writing the code below (risky!). So the compiler won't take care about it.
makes!: any[]
More specifically,
Your code's design warrants improvement. Best practices discourage the use of locally defined and mutable variables. A more effective approach involves managing data storage within a service:
Here's an illustrative example of the concept I'm proposing. Please note that I have not tested it, and there is room for further improvement.
type Make = any // Type it
class MakeService {
private readonly source = new BehaviorSubject<Make[] | undefined>(undefined);
loading = false;
private readonly getMakes = (): Observable<Make[]> => {
/* ... your current implementation */
};
readonly getMakes2 = () => {
if (this.source.value) {
return this.source.asObservable();
}
return new Observable(_ => _.next()).pipe(
tap(_ => {
this.loading = true;
}),
mergeMap(this.getMakes),
mergeMap(data => {
this.source.next(data);
return data;
}),
tap(_ => {
this.loading = false;
}),
catchError((err: any) => {
this.loading = false;
return throwError(err);
}),
);
};
}
@Component({
selector: 'app-vehicle-form',
template: `
<div *ngIf="makeService.loading">Loading...</div>
<div *ngFor="let vehicule of vehicules | async">
{{vehicle.name}}
</div>
`,
styleUrls: ['./vehicle-form.component.css']
})
export class VehicleFormComponent implements OnInit {
constructor(public makeService: MakeService) {}
readonly makes = this.makeService.getMakes2().pipe(
tap(makes => console.log('MAKES', makes))
);
readonly vehicules = this.makes.pipe(
map(make => make/* some transformation */),
tap(vehicule => console.log('VEHICLE', vehicule)),
);
ngOnInit() {
this.makes.subscribe(makes => {
console.log('MAKES', makes);
});
}
}
Upvotes: 92
Reputation: 22745
This has been discussed in Angular Github at https://github.com/angular/angular/issues/24571
I think this recommendation from angular gives good advice:
For angular components, use the following rules in deciding between:
a) adding initializer
b) make the field optional?
c) leave the!
- If the field is annotated with
@input
- Make the field optional b) or add an initializer a).- If the input is required for the component user - add an assertion in
ngOnInit
and apply c).- If the field is annotated
@ViewChild
,@ContentChild
- Make the field optional b).- If the field is annotated with
@ViewChildren
or@ContentChildren
- Add back '!' - c).- Fields that have an initializer, but it lives in
ngOnInit
. - Move the initializer to the constructor a).- Fields that have an initializer, but it lives in
ngOnInit
and cannot be moved because it depends on other@input
fields - Add back '!' - c).
Upvotes: 8
Reputation: 124
A little late for the party. but the most voted answer includes changing the security level as a whole abandoning type safety, to work around this use the typing mechanism of the typescript itself
@ViewChild(MatPaginator) paginator: MatPaginator | undefined;
ngAfterViewInit() {
this.dataSource.paginator = this.paginator as MatPaginator;
}
I believe the angular material documentation should be updated, but until then I believe this is the best way to do it
Upvotes: 1
Reputation: 921
Go to tsconfig.json
add "strictPropertyInitialization": false, in "compilerOptions":
Upvotes: 43
Reputation: 23
Just declare your variable as type any
@Input() headerName: any;
Upvotes: -3
Reputation: 338
It is because typescript 2.7.2 included a strict class checking where all properties should be declared in constructor. So to work around that, just add an exclamation mark (!) like:
name!:string;
Upvotes: 9
Reputation: 31
Instead of turning off the strict mode, you can assign a initial value to the variable. For example:
makes: any[] = [null];
private year: number = 0;
Upvotes: 0
Reputation: 18518
Just go to tsconfig.json and set
"compilerOptions": {
"strictPropertyInitialization": false,
...
}
to get rid of the compilation error.
Otherwise you need to initialize all your variables which is a little bit annoying
Upvotes: 1661
Reputation: 173
Follow 2 steps to solve this::
Upvotes: 0
Reputation: 1310
if you don't want to change your tsconfig.json, you can define your class like this:
class Address{
street: string = ''
}
or, you may proceed like this as well:
class Address{
street!: string
}
by adding an exclamation mark "!" after your variable name, Typescript will be sure that this variable is not null or undefined.
Upvotes: 14
Reputation: 441
In my case it works with different declaration according new typescript strict features:
@ViewChild(MatSort, {static: true}) sort!: MatSort;
If disable typescript new strict feature in tsonfig.json with
"compilerOptions": {
///
,
"strictPropertyInitialization":false
}
the old Angular's guide code works well
@ViewChild(MatSort) sort: MatSort;
Here is 4 ways to solve the issue thanks to Arunkumar Gudelli (2022) https://www.angularjswiki.com/angular/property-has-no-initializer-and-is-not-definitely-assigned-in-the-constructor/
Upvotes: 6
Reputation: 377
1) You can apply it like the code below. When you do this, the system will not give an error.
"Definite Assignment Assertion" (!) to tell TypeScript that we know this value
@Injectable()
export class Contact {
public name !:string;
public address!: Address;
public digital!: Digital[];
public phone!: Phone[];
}
2) The second method is to create a constructor and define values here.
export class Contact {
public name :string;
public address: Address;
public digital: Digital[];
public phone: Phone[];
constructor( _name :string,
_address: Address,
_digital: Digital[],
_phone: Phone[])
{
this.name=_name;
this.address=_address;
this.digital=_digital;
this.phone=_phone;
}
}
3) The third choice will be to create a get property and assign a value as follows
export class Contact {
public name :string="";
public address: Address=this._address;
get _address(): Address {
return new Address();
}
}
Upvotes: 6
Reputation: 197
You can also add @ts-ignore to silence the compiler only for this case:
//@ts-ignore
makes: any[];
Upvotes: -2
Reputation: 12206
demo code
class Plant {
name: string;
// ❌ Property 'name' has no initializer and is not definitely assigned in the constructor.ts(2564)
}
class Plant {
name: string | undefined;
}
class Plant {
name!: string;
}
class Plant {
name: string = '';
}
class Plant {
name: string;
constructor() {
this.name = '';
}
}
class Plant {
name: string;
constructor(name: string) {
this.name = name ?? '';
}
}
5
👍class Plant {
constructor(public name: string = name ?? '') {
//
}
}
not recommended
{
"compilerOptions": {
+ "strictPropertyInitialization": false,
- "strictPropertyInitialization": true,
}
}
https://www.typescriptlang.org/docs/handbook/2/classes.html#--strictpropertyinitialization
Upvotes: 49
Reputation: 9656
in tsconfig.json file , inside "compilerOptions" add :
"strictPropertyInitialization": false,
Upvotes: 10
Reputation: 292
JUST go to the tsconfig.ts and add "strictPropertyInitialization": false, into the compilerOptions Object .
EXAMPLE:
"compilerOptions" : {
"strictPropertyInitialization": false,
}
Upvotes: 2
Reputation: 1380
Add these two line on the tsconfig.json
"noImplicitReturns": true,
"strictPropertyInitialization": false,
and make sure strict is set to true
Upvotes: 2
Reputation: 439
a new version of typescript has introduced strick class Initialization, that is means by all of the properties in your class you need to initialize in the constructor body, or by a property initializer. check it in typescript doccumntation to avoid this you can add (! or ?) with property
make!: any[] or make? : any[]
otherwise, if you wish to remove strick class checking permanently in your project you can set strictPropertyInitialization": false in tsconfig.json file
"compilerOptions": { .... "noImplicitReturns": false, .... "strictPropertyInitialization": false },
Upvotes: 8
Reputation: 222712
I think you are using the latest version of TypeScript. Please see the section "Strict Class Initialization" in the link
.
There are two ways to fix this:
A. If you are using VSCode you need to change the TS version that the editor use.
B. Just initialize the array when you declare it
makes: any[] = [];
or inside the constructor:
constructor(private makeService: MakeService) {
// Initialization inside the constructor
this.makes = [];
}
Upvotes: 388
Reputation: 9333
Go to your tsconfig.json
file and change the property:
"noImplicitReturns": false
and then add
"strictPropertyInitialization": false
under "compilerOptions"
property.
Your tsconfig.json
file should looks like:
{
...
"compilerOptions": {
....
"noImplicitReturns": false,
....
"strictPropertyInitialization": false
},
"angularCompilerOptions": {
......
}
}
Hope this will help !!
Good Luck
Upvotes: 131
Reputation: 299
A batter approach would be to add the exclamation mark to the end of the variable for which you are sure that it shall not be undefined or null, for instance you are using an ElementRef which needs to be loaded from the template and can't be defined in the constructor, do something like below
class Component {
ViewChild('idname') variable! : ElementRef;
}
Upvotes: 16
Reputation: 460
Put a question (?) mark after makes variable.
makes?: any[];
vehicle = {};
constructor(private makeService: MakeService) { }
It should now works. I'm using angular 12 and it works on my code.
Upvotes: 14
Reputation: 1359
You can declare property in constructor like this:
export class Test {
constructor(myText:string) {
this.myText= myText;
}
myText: string ;
}
Upvotes: 1
Reputation: 23088
Another way to fix in the case when the variable must remain uninitialized (and it is dealt with at the run time) is to add undefined
to the type (this is actually suggested by VC Code). Example:
@Input() availableData: HierarchyItem[] | undefined;
@Input() checkableSettings: CheckableSettings | undefined;
Depending on actual usage, this might lead to other issues, so I think the best approach is to initialize the properties whenever possible.
Upvotes: 6
Reputation: 39
Next to variables "?" You can fix it by putting it.
Example:
--------->id?:number --------->name?:string
Upvotes: -4
Reputation: 644
Update for 2021 :
there is property like "strictPropertyInitialization"
Just go to tsconfig.json and set
"strict": false
to get rid of the compilation error.
Otherwise you need to initialize all your variables which is a little bit annoying.
reason behind this error is :
Upvotes: 27