0

I find such code in Element plus source code.

instances[i].vm.component!.props.offset = pos

i do not know what is mean of "!." , and I try to search it in Google, but I can't get any effective answer.

peter roe
  • 21
  • 6
  • 1
    It's TypeScript, not JavaScript. See the [linked question's answers](https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci) for details. (Basically: It tells TypeScript that `instances[i].vm.component` is not `undefined` or `null` even though the type information says it is. In general, avoid it in favor of a type assertion function.) – T.J. Crowder Nov 13 '21 at 13:27
  • In a comment on an answer that will be deleted soon, you said you thought this was like `?.` in JavaScript. It isn't, they mean very different things. `x!.y` is `x.y` at runtime, the `!` is purely to tell TypeScript that even though the type of `x` may allow it to be `undefined` or `null`, you're *asserting* (claiming) that it isn't and it's okay to use `x.y`. If the assertion is wrong (`x` is `null` or `undefined`), you'll get a runtime error. In contrast, `x?.y` is a runtime construct and means something completely... – T.J. Crowder Nov 13 '21 at 13:51
  • ...different: if `x` is `null` or `undefined`, the result of `x?.y` is `undefined` (not an error). – T.J. Crowder Nov 13 '21 at 13:51
  • So the difference of "!." is that errors will be reported, can i say that ? – peter roe Nov 13 '21 at 13:59
  • I wouldn't put it that way. Let's say you have `const y = x!.y;` That means "I know `x` won't be `null` or `undefined` ("nullish"), so I'm going to use `x.y`." (You'll get an error if you're wrong.) In contrast, `const y = x?.y;` means "`x` may be nullish, so I want `y` to be `undefined` if `x` is nullish, or `x.y` if `x` isn't nullish." They're different things you'd do different places (although again, I would avoid using `!` at all; instead, guard the code with a [type assertion function](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions))... – T.J. Crowder Nov 13 '21 at 14:03
  • ...or just an `if`, depending on the situation. – T.J. Crowder Nov 13 '21 at 14:06
  • I thought for a few minutes,I know the different of them. I am sure `x` is not nullish, so I use `x!.y`. and if I am sure `x` maybe nullish, so I use `x?.y` maybe more appropriate – peter roe Nov 13 '21 at 14:13
  • Right -- except (again) I would avoid using `!`. If you do a guard (like `if (x)`), then within the `if `block TypeScript will know `x` is not nullish and won't require the `!` to access `y`: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgBrIN4Chm+QTwC5kQBXAWwCNoBuLAXywQHsQBnMZAD2PQB9kpEABMIMUBGHIAvMgCycMAAsAdFDgjm5ABQBKZAB5kABhUBWHHgD8mIsgAsAJkZ5kxIaPEhJdLAHo-ZAAJaAgAGmQAAy5I5HI4fGRqKI8xCWFIiLZmZAAVfAAHCABlBChgAs4Ad2ZSABspNgSorhV8WKq4NmQNZGgoZihkbXjE5NSvSV0sYBhhrn1sVxZ2ZjqIFTrmAHNtVvxdGjwA4NCI6NjgboBrEGYqkBJmTjAc5MiJ9MiGIA – T.J. Crowder Nov 13 '21 at 14:19

0 Answers0