5 TypeScript Tips That Will Save You Hours

·By Abhiram Chakkiyar
#typescript#javascript#programming

TypeScript has become the standard for writing type-safe JavaScript. But are you using it to its full potential?

1. Use unknown Instead of any

// Bad
function parse(data: any) {
  return JSON.parse(data);
}

// Good
function parse(data: unknown) {
  if (typeof data === 'string') {
    return JSON.parse(data);
  }
  throw new Error('Expected string');
}

2. Discriminated Unions

type Success = { status: 'success'; data: string };
type Error = { status: 'error'; error: string };

type Result = Success | Error;

function handleResult(result: Result) {
  if (result.status === 'success') {
    console.log(result.data); // TypeScript knows this exists!
  }
}

3. Utility Types to the Rescue

// Make specific properties optional
type PartialUser = Partial<User>;

// Pick specific properties
type UserEmail = Pick<User, 'email' | 'name'>;

// Omit specific properties
type CreateUser = Omit<User, 'id'>;

4. Type Guards

function isString(value: unknown): value is string {
  return typeof value === 'string';
}

function process(value: unknown) {
  if (isString(value)) {
    console.log(value.toUpperCase()); // TypeScript knows it's a string!
  }
}

5. Template Literal Types

type Color = 'red' | 'blue';
type Quantity = 'light' | 'dark';

type Shade = `${Quantity}-${Color}`;
// 'light-red' | 'light-blue' | 'dark-red' | 'dark-blue'

Bonus: Use Strict Mode

Always enable strict mode in tsconfig.json:

{
  "compilerOptions": {
    "strict": true
  }
}

This catches many common errors at compile time!

Conclusion

These tips will help you write safer, more maintainable TypeScript code. Start using them today and see the difference.


What are your favorite TypeScript tips? Let me know on Twitter!

AC

Written by Abhiram Chakkiyar

Developer, writer, and creator. I write about code, technology, and everything in between.