Skip to content

Build Your First TypeScript File

Video Lecture

Build Your First TypeScript File Build Your First TypeScript File

Basic JavaScript File

Create yourself working a folder somewhere, and next create a new file in it called foo.js

1
2
3
4
5
6
7
function foo(bar) {
    return 'Hello, ' + bar
}

let baz = 'ABC'

console.log(foo(baz))

We can run this file directly in Nodejs or include it in a web page, and it will run inside the browser. But this isn't typescript.

Test it,

node foo.js

The TypeScript Equivalent

img/tsc-outline.png

Rename the foo.js to foo.ts

1
2
3
4
5
6
7
function foo(bar) {
    return 'Hello, ' + bar
}

let baz = 'ABC'

console.log(foo(baz))

We can then compile it

tsc foo.ts

And then we can run it using Node.

node foo.js

The output will be

Hello, ABC

That is your very first typescript file.

ECMAScript Editions

https://www.w3schools.com/js/js_versions.asp

NodeJS ECMAScript Compatibility Chart

https://node.green/

Metasyntactic Variables

I use metasyntactic variable names in this crash course.

Examples are foobar, foo, bar, baz, qux, quux, quuz, corge, grault, garply, waldo, fred, plugh, xyzzy, and thud.

They don't mean anything special, they are just good for using in code examples.

https://en.wikipedia.org/wiki/Metasyntactic_variable

https://www.typescriptlang.org/docs/handbook/compiler-options.html