DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)
  • Supercharge AI Workflows on Azure: Remote MCP Tool Triggers + Your First TypeScript MCP Server
  • TypeScript in Cloud Applications: Why It’s a Powerful Choice

Trending

  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Securing the AI Host: Spring AI MCP Server Communication With API Keys
  • Zero-Downtime Deployments for Java Apps on Kubernetes
  • Stop Debugging Glue Jobs Manually: Building an Agentic Observability Layer for Data Pipelines
  1. DZone
  2. Coding
  3. JavaScript
  4. Creating Custom Types in TypeScript

Creating Custom Types in TypeScript

Let's look at how to create custom types in TypeScript

By 
Johnny Simpson user avatar
Johnny Simpson
·
Mar. 09, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

JavaScript is a weakly typed language, which means we don't usually think about types. Typescript is strongly typed, which means everything has a type.

Occasionally we want to make an object or the return of a function conform to a certain format. This is where we use custom types. Typescript allows us to define our own custom types, which we can then use in our code.

Why Would We Use Custom Types?

Suppose you have a function that always returns data in a certain format, and uses an API to get that data. If the API returns data in the wrong format, we probably don't want the wrongly formatted data to end up in our code where it could cause issues. In such a case, we might ask that the return of a function conforms to a certain type. As such, we would define our own type.

Alias Types

One example of how to create types is called an alias type. An example of how we define a type is shown below:

TypeScript
 
type Company = {
    name: string,
    address: string,
    value?: number
}


If we give something the type Company, then we expect it to have at least a name and address, and an optional value, which does not have to be given. As you can see, having a question mark in our type denotes that this property is optional.

If we were to use this in code, we might do something like this:

TypeScript
 
let myFunction = async function(): Promise<Company> {
    let getApi = await fetch('/myApi/data', {
        method: 'GET'
    })
    let getResult:Company = await getApi.json();
    return getResult;
}


In the above code, we are returning a Promise of type Company, and if we don't get that, we'll get an error. So for example, if we try to run this and we don't get an address or name back from our API, we will have an error that we can handle.

Extending Alias Types

You can extend alias types, i.e. if you want to add a new element to it. For example:

TypeScript
 
type Company = {
    name: string,
    address: string,
    value?: number
}

type SubCompany = Company & {
    identity: string
}


In the above, SubCompany will have everything Company has, plus a required attribute called identity.

Using Interfaces instead

Everything we've spoken about so far has been using the type keyword, but we can do the same stuff using the interface keyword instead. It is really personal preference which one you use. Our example above looks like this with interface:

TypeScript
 
interface Company {
    name: string,
    address: string,
    value?: number
}

interface SubCompany extends interface {
    identity: string
}


Union Types

We can also define custom types using a much simpler syntax known as union types. Let's say we have a type which is either going to be a string or number, called myType. We could define that type as shown below:

TypeScript
 
type myType = number | string


Literal Types

This is where we set a type that has a specific list of values it can select from. Let's say our original type, Company, can only have three values, red, blue, or green. We can define a literal type, and use that as the type of our name attribute:

TypeScript
 
type Option = "blue" | "green" | "red" 
type Company = {
    name: Option,
    address: string,
    value?: number
}


TypeScript

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)
  • Supercharge AI Workflows on Azure: Remote MCP Tool Triggers + Your First TypeScript MCP Server
  • TypeScript in Cloud Applications: Why It’s a Powerful Choice

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook