Fastest way to start a typescript project
Modern typescript tooling has come a long way. See how to setup a new project with TS-Node, TSC, and typeconfig.json.
Typescript sometimes has a reputation for being difficult to get started with. Modern tooling has really improved this situation and it is now very easy to create a new project and get up and running in no time.
Create a new project (in 30 seconds)
In a terminal on Mac OSX or Linux create a new directory and navigate to it using this command:
mkdir example && cd $_
Setup node
You need a package.json
file to run a typescript project. Use npm init
to create one:
npm init --yes
The output shows a newly created package.json
file:
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Install typescript
Next save typescript
compiler and the loader ts-node
as development dependencies.
npm i -D typescript ts-node
The shell output shows the installed dependencies (with your version instead)
+ ts-node@10.0.0
+ typescript@4.3.4
added 14 packages from 45 contributors and audited 14 packages in 1.209s
found 0 vulnerabilities
Setup tsconfig
Next you can create a tsconfig.json
file using typescript's tsc --init
command:
./node_modules/.bin/tsc --init
A success message is shown in the terminal:
message TS6071: Successfully created a tsconfig.json file.
Create a typescript file
Now we can create a simple typescript file called index.ts
echo 'console.log("Hi")' > index.ts
Run the script
You can run typescript using NodeJS with the ts-node/esm
loader.
Run your script like so:
node --loader ts-node/esm index.ts
The output shows our message:
Hi
Next steps
You can now write and run typescript files easily. To make run commands easier try adding the scripts to the package.json file:
{
"scripts": {
"start": "node --loader ts-node/esm index.ts"
}
}
Then run your script using npm start
.
Why use Typescript?
Typescript is a superset of standard Javascript that adds a powerful type system to the Javascript you already know. It is totally optionally but can dramatically reduce compile-time bugs and exceptions due to the information that type systems can provide to a compiler and the process of compiling the typescript into javascript before running it. This step provides an extra step of logic validation before running your javascript. TS-Node is a helpful tool for running the compilation step inside node itself so that you can skip the build stage. MailSlurp uses Typescript to create email testing APIs for NodeJS. Send and receive emails and attachments from test email accounts using MailSlurp for free!
Related content
Blog
MailSlurp technical blog from the developers. Posts about email API testing and features added to the platform and new e...
MailSlurp Examples
Developer example projects and guides are available in many languages and frameworks.
Email testing and programming guides
Learn how MailSlurp works and how you can use it to achieve email related tasks using the official Javascript client.
Javascript email libraries for MailSlurp
Nodemailer alternatives to send and receieve email in code and tests
Fastest way to start a typescript project
Modern typescript tooling has come a long way. See how to setup a new project with TS-Node, TSC, and typeconfig.json.
Fetch error message javascript
Handle response exceptions with Fetch in Javascript
GraphQL API disposable email accounts
How to create real email addresses using GraphQL to send and receive emails in tests and frontend applications.
GraphQL Email API Tutorial
Did you know you can send and receive emails using GraphQL?
Hugo responsive image srcsets
Serve responsive picture tags with custom render-image layout partial in Hugo static site generator.
Markdown Heading Sizes
How to specify headers h1, h2, h3 and more in Github flavoured Markdown
NodeMailer NPM Tutorial
Send and receive email using NodeMailer in Node JS.
Create custom print classes with Tailwind
Configure tailwind.config.js to create prefixed classes with a custom media query.
Send and receive email in Cypress JS tests with MailSlurp
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
Cypress Email Plugin - Test email accounts in Javascript (an...
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Send email with Axios JS (and receive email too!)
Send email in Javascript with MailSlurp email API
Test email accounts with Jest and Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
Test user sign-up with NodeJS and WebDriver
Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.
TestCafe end-to-end MFA testing for user sign-up and email v...
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
Base64 file uploads
How to encode files as Base 64 encoded strings in several languages
Deno Email Apis
Send and receive emails in Deno JS. Use APIs in Deno with MailSlurp.
Email read (opened seen settings)
How to control the seen or read settings for an email using MailSlurp.
GraphQL Email API
Fetch and read emails with GraphQL inboxes using MailSlurp email API.
Testing OTP password link username and password for 2 factor...
Testing OTP password link username and password for 2 factor authentication (2FA)
MailSlurp NodeMailer SMTP Usage
Use SMTP nodemailer with MailSlurp disposable email addresses
Testing email with Cypress test email accounts
Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.
Send emails in NodeJS using SMTP
How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS