0

In one of the react codebase, I came across a code like below. What does it do? The code is using typescript + react

interface ComponentProps {
   getLibraryCard?(): void
}
componentDidMount() {
   this.props.getLibraryCard!()
}

What does the BANG operator do here?

Vishnu Mohan
  • 111
  • 5
  • Not null assertion. It's from TypeScript, not JavaScript. – VLAZ May 28 '20 at 09:31
  • `?` is pretty new, the statement resolves to undefined if `getLibraryCard` is undefined (instead of throwing an error). `!` will execute, but you tell typescript that, yes, your sure that this is not null, because you know it (but it will throw an error at runtime if you lied and it's null or undefined ;) - it just silences a compiler check) – Andreas Dolk May 28 '20 at 09:56
  • Cool, Thanks for the help@VLAZ and @Andreas – Vishnu Mohan May 28 '20 at 10:01

0 Answers0