in an NPM module I use typescript
"devDependencies": {
"@types/node": "^8.0.0",
"typescript": "^2.8.1"
}
and I want to return a private nullable parameter using a public method. Please refer to the axample below. The error I see is
Property 'string1' has no initializer and is not definitely assigned in the constructor.
If I assign an undefinded in the constructor I got the error
[ts]
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'
How should I do this in typescript, I´m from the c# side :)
export class HowToDoThis {
private string1?: string;
public constructor() {
//this.string1 = undefined;
}
public add2String1(content: string) {
this.string1 += content;
}
public getString1(): string {
return this.string1;
}
}
Type 'null' is not assignable to type 'string'. Note that TypeScript treats null and undefined differently in order to match JavaScript semantics.
To declare a function with an object return type, set the return type of the function to an object right after the function's parameter list, e.g. function getObj(): {name: string;} {} . If the return type of the function is not set, TypeScript will infer it.
TypeScript Nullable is a special type null that has the value null. TypeScript Null is much like void, i.e. not useful on its own. By default, null is a subtype of all other subtypes which means a user can assign null to any of the data types like string, number, etc. It also means that the user can not stop the null type from being assigned ...
It is easy to add a null value to an interface. with enabling this, assigning null to an interface throws an error Type ‘null’ is not assignable to type ‘employee’. (2322) The solution is to add the union type with null. An interface can have properties of different types. Let’s add how to add nullable types to these properties in typescript.
Previously in TypeScript, it was not possible to explicitly name these types as “null” and “undefined”. However, it can now be used regardless of the type checking mode. To assign “undefined” to any property, the –strictNullChecks flag has to be turned off.
‘Type ‘null’ is not assignable to type ‘number’. This error above would not appear if -strictNullChecks is OFF, and hence TypeScript compiler will allow the compilation of the code. Flag -strictNullChecks when OFF protects code from referencing null values in the code.
You can define
private string1: string | undefined;
The error message you are getting Type 'string | undefined' is not assignable to type 'string'
isn't because you assigned this.string = undefined
in the constructor, it is because you defined getString1()
as returning string
and you didn't check that this.string1
was in fact a string.
You can either change getString1()
so it does in fact always return a string, or you could make it return string|undefined
, or much simpler you could just initialise string1
to an empty string and never have it undefined.
So this works:
export class HowToDoThis {
private string1?: string;
public constructor() {
this.string1 = undefined;
}
public add2String1(content: string) {
this.string1 += content;
}
public getString1(): string {
return this.string1 || "";
}
}
But this would be better if only because calling add2String1('foo')
won't give you the string 'undefinedfoo'
:
export class HowToDoThis {
private string1: string = "";
public add2String1(content: string) {
this.string1 += content;
}
public getString1(): string {
return this.string1;
}
}
And this is best of all (don't use getter functions in typescript, you can always create a get
property later if you need to do something when a property is accessed):
export class HowToDoThis {
public string1: string = "";
public add2String1(content: string) {
this.string1 += content;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With