Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

Nullcheck on nullable Types in Typescript

I'm new to typescript, with the following code interface definition.

export Interface Response {
  content?: {
    // ...
  }
}

let response: Response | null = something();

How to access content without doing null checks, which i assume is not at all the best approach.

if (response != null) {
  if (response.content != null) {
    content = (response.content as MyContent);
  }
}
question from:https://stackoverflow.com/questions/65938330/nullcheck-on-nullable-types-in-typescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

content = response?.content == null ? null : response.content as MyContent;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...