You can simply use typeof. It will check undefined, null, 0 and "" also.
if(typeof RetailPrice!='undefined' && RetailPrice){
return this.RetailPrice;
}
Answer from Unknown on Stack OverflowYou can simply use typeof. It will check undefined, null, 0 and "" also.
if(typeof RetailPrice!='undefined' && RetailPrice){
return this.RetailPrice;
}
To excludes blank strings as well
if(this.retailPrice && this.retailPrice.trim()){
//Implement your logic here
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
What do people think of the ability to have multiple types like this? I found it super annoying when mapping between objects. If the same property names are used but one is string but the other property from the other class is string | null, the IDE complains. Are there any situations where you've found it helpful to be able to declare at type like this?
All fields in JavaScript (and in TypeScript) can have the value null or undefined.
You can make the field optional which is different from nullable.
interface Employee1 {
name: string;
salary: number;
}
var a: Employee1 = { name: 'Bob', salary: 40000 }; // OK
var b: Employee1 = { name: 'Bob' }; // Not OK, you must have 'salary'
var c: Employee1 = { name: 'Bob', salary: undefined }; // OK
var d: Employee1 = { name: null, salary: undefined }; // OK
// OK
class SomeEmployeeA implements Employee1 {
public name = 'Bob';
public salary = 40000;
}
// Not OK: Must have 'salary'
class SomeEmployeeB implements Employee1 {
public name: string;
}
Compare with:
interface Employee2 {
name: string;
salary?: number;
}
var a: Employee2 = { name: 'Bob', salary: 40000 }; // OK
var b: Employee2 = { name: 'Bob' }; // OK
var c: Employee2 = { name: 'Bob', salary: undefined }; // OK
var d: Employee2 = { name: null, salary: 'bob' }; // Not OK, salary must be a number
// OK, but doesn't make too much sense
class SomeEmployeeA implements Employee2 {
public name = 'Bob';
}
To be more C# like, define the Nullable type like this:
type Nullable<T> = T | null;
interface Employee{
id: number;
name: string;
salary: Nullable<number>;
}
Bonus:
To make Nullable behave like a built in Typescript type, define it in a global.d.ts definition file in the root source folder. This path worked for me: /src/global.d.ts
Hi there! Quick question to the functional buffs out there:
What's the advantage of using a Maybe<string> type over using string | null? With strict null checking you still need to handle the null case, but you avoid using external libraries and having to introduce new concepts to the other developers on the team.
Same for Either<Error, string> and string | Error.
I think it's mostly a matter of preference. Folks coming from Scala/Haskell invariable reach for an Option/Maybe, nearly everyone else uses the if/else.
Scala and Haskell have distinct syntax to make dealing with Maybe a little more concise, TypeScript doesn't at least in part because in most cases Maybe is redundant with strict null checks.
IMO, it's more idiomatic TypeScript to rely on type guards with strict null checks.
I would definitely prefer null. It's just more idiomatic TS. In Scala, the language is built around Option. In TS, it's built around null and undefined.
For example, look at optional chaining. That works with null, not Maybe.