ajitid / fzf-for-js

Do fuzzy matching using FZF algorithm in JavaScript
https://fzf.netlify.app
BSD 3-Clause "New" or "Revised" License
888 stars 21 forks source link

[question] What does the postfix ! do? #30

Closed dotnetCarpenter closed 3 years ago

dotnetCarpenter commented 3 years ago

Hi, congratz on being on javascript weekly.

I skimmed the source and found str.split("").map((s) => s.codePointAt(0)!);.

What is the postfix ! for? I tried to copy the line and are getting a syntax error in nodejs REPL. Is it a typescript thing?

Cheers!

ajitid commented 3 years ago

Hey! Thanks, only yesterday I found out that this library is mentioned in the newsletter.

What is the postfix ! for? ... Is it a typescript thing?

Yes, this is a TypeScript thing. codePointAt can return undefined if it doesn't find a character at that index. By appending ! we are telling TypeScript that it is safe to assume that the returned value from codePointAt is a non-null, non-undefined value.

Try hovering over strOrNull and onlyStr in this TS playground to see how it works.

Hope that helps!

dotnetCarpenter commented 3 years ago

Thanks @ajitid!