Logo
BlogDashboardAboutProjects
Logo
HomeBlogDashboardAboutProjects
GithubGithub
LinkedinLinkedin
TwitterTwitter
v1.12.1
23 Nov 2023
·
2 min read

What the ! operator does in TypeScript and Swift

The ! at the end of a TypeScript expression tells the compiler you know the value isn't null or undefined. It's the same idea as force unwrapping in Swift. Useful, but worth understanding before reaching for it.

So what is the ! in TypeScript?

This is actually known as a non-null assertion operator.

Take for example the following code:

type Item = {
  title: string;
  value: string;
};

const items: Item[] = [
  { title: "Foo", value: "Foo" },
  { title: "Bar", value: "Bar" },
  { title: "Alpha", value: "Alpha" },
];

// Exclamation mark at the end of the Array.find() function
const getAlpha = items.find((item) => item.title === "Alpha")!;

The following will return a type Item | undefined. In some scenarios, we are not allowed to parse an undefined value and therefore we need to write extra code just to handle the undefined value first.

One of the most common way is to do the following.

if (getAlpha) {
  return <SomeComponent alpha={getAlpha} />;
}

Or we could type cast it back to the Item type like the following.

<SomeComponent alpha={getAlpha as Item} />

However, with the ! in TypeScript. We are able to just do the following and return the type Item quickly.

const getAlpha = items.find((item) => item.title === "Alpha")!;

The use of ! however does not remove the fact that the Array.find() will not return a null or undefined at all.

Swift

Swift is used as the programming language for creating apps on iOS, macOS, watchOS.

Since I have created an Augmented Reality(AR) App in Swift before, the ! works very similarly and is very commonly used.

The ! in Swift is used to force unwrapping of optionals.

Example:

let title: String? = "Foo"

if title != nil {
    var something = title!
}

So as you have guessed it, the ! actually forces the optional value in the variable name and gain access to the String value.

Summary

And that is all for this post. Just wanted to share something regarding the ! operator that I discovered while writing code in Swift and now being able to also use it in TypeScript.

Related Articles

11 Nov 2023
2 tags
process.env vs import.meta: environment variables in Node and the browser
process.env only runs in Node. It won't work in the browser. import.meta is the module-aware alternative, and it works with TypeScript too.
23 Jan 2023
1 tag
Adding JSON-LD structured data to your React blog
Google uses structured data to understand what's on your page. JSON-LD is the recommended format. Here's a minimal React component to add it, and how to verify it works.
19 Jan 2023
1 tag
Using the Web Share API to trigger the OS share sheet
Instead of building a custom share UI, you can hand off to the OS share sheet with one function call. navigator.share() works on mobile browsers. Here's how to use it.
29 Mar 2023
1 tag
Feature flags: ship code without shipping features
A feature flag is an if/else around a piece of code, controlled by an environment variable. It lets you merge and deploy work-in-progress without turning it on. Fewer conflicts, safer releases.