@Column({ name: 'device_kind', type: 'int2', nullable: false })
deviceKind?: number;
Can anyone explain this code? I didn't understand why they added the '?' mark. and some of them has '!' instead of question mark. What do they mean?
@Column({ name: 'device_kind', type: 'int2', nullable: false })
deviceKind?: number;
Can anyone explain this code? I didn't understand why they added the '?' mark. and some of them has '!' instead of question mark. What do they mean?
This is a really a Typescript question, not TypeORM.
When you define a property like this:
type Foo = {
prop1?: number
}
You are saying prop1
is optional.
When a property is preceded with !
it means you are telling Typescript to not warn you that you didn't initialize it in the constructor (which it normally will complain about in strict mode).
Example:
class Foo {
// Typescript does not complain about `a` because we set it in the constructor
public a: number;
// Typescript will complain about `b` because we forgot it.
public b: number;
// Typescript will not complain about `c` because we told it not to.
public c!: number;
// Typescript will not complain about `d` because it's optional and is
// allowed to be undefined.
public d?: number;
constructor() {
this.a = 5;
}
}
It should be noted that the c!
case in the above class is really a way to tell Typescript: "I know what I'm doing, I know I'm setting this somewhere, just not in the constructor. Please don't complain".
This is the not the same as the d?
case, because this just means that d
is allowed to be a number
or undefined
.