days
-1
-8
hours
0
-2
minutes
-5
-2
seconds
-2
0
search
Three is the magic number

TypeScript 3.0: Get to know the unknown type

Sarah Schlothauer
typescript
© Shutterstock / s-ts

Have you heard? TypeScript 3.0 is here. What new features are behind the door? Find out what changes have been made and how to best utilize them.

TypeScript has seen a surge in popularity and its fans are anything but quiet about their love for the open source programming language. It is a scalable, syntactical superset of JavaScript that uses already existing JavaScript syntax to work its magic. By using already available JavaScript libraries and code, it’s easy to pick up for those who are already familiar with JavaScript.

Recently, we saw TypeScript soar up the TIOBE top 100 list from #93 all the way up to a humble, yet impressive #50. If this trend continues, TypeScript will soon be nestling alongside other popular programming languages, so it’s worth keeping your radar on.

TypeScript 3.0: Third version is the charm

Version 3.0 was announced on July 30, 2018. Let’s look at the release notes and see what the shiny new features do. Interesting, the release notes state that there are few code breaking features in 3.0, so upgrading will be a breeze.

Project References

With the new project reference feature, TypeScript projects can depend on other TypeScript projects; tsconfig.json files can now reference other tsconfig.json files.

This will make your code more manageable by splitting it into smaller projects for faster build times.

Here’s some sample code from the announcement blog to demonstrate how this is used:

// ./src/bar/tsconfig.json
{
    "compilerOptions": {
        // Needed for project references.
        "composite": true,
        "declaration": true,

        // Other options...
        "outDir": "../../lib/bar",
        "strict": true, "module": "esnext", "moduleResolution": "node",
    },
    "references": [
        { "path": "../foo" }
    ]
}

The new game players are references and composite. References specifies the tsconfig.json files, while the composite field “ensures certain options are enabled so that this project can be referenced and build incrementally for any project that depends on it”. Using these in conjunction will be a game changer for code that has several different build steps.

SEE ALSO: TypeScript: Everyone’s type of JavaScript

Another use of project references is the ability to map your input source to its outputs.

Want to learn more about this new feature? The issue tracker on GitHub is a great place to dig deeper.

Tuple Types

TypeScript 3.0 will now extract and infer generics as tuple types by using the call function.

Tuples have also been expanded in what they can do. Tuples now allow for rest elements at the end, trailing optional elements, and can even be empty.

Improved error messages

Previously, error messages were a little…vague. Now, the error messages will help you figure out how to best fix the issue and provides related error spans.

No more mystery when checking over your code! The error messages themselves are also cleaner and smarter.

Unknowntype

Meet the unknown type. Mysterious name aside, it does what the any type cannot. If you wish to describe the least-capable type, now you can!

“TypeScript 3.0 introduces a new type called unknown that does exactly that. Much like any, any value is assignable to unknown; however, unlike anyunknown is assignable to almost nothing else without a type assertion. You also can’t access any properties off of an unknown, nor can you call/construct them.”

SEE ALSO: TypeScript tutorial: Basics and typing in JavaScript

This is just the tip of the iceberg. Despite this not being a very code-breaking update, there are still several more shiny features to explore including:

  • Support for defaultProps in JSX
  • /// <reference lib="..." /> directives
  • Named import refactorings
  • Closing JSX tag completions and outlining spans
  • Quick fixes for unreachable code and unused labels

If you haven’t used TypeScript yet, there’s no better time to start than with a new release. Check out version 3.0 and see if its magic will put a spell on you!

Author
Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is an assistant editor for JAXenter.com. She received her Bachelor's degree from Monmouth University in Long Branch, New Jersey and is currently enrolled at Goethe University in Frankfurt, Germany where she is working on her Masters. She lives in Frankfurt with her husband and cat.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of