TypeScripts type system is extremely powerful in that you can do supremely powerful things in the language moving / slicing / dicing types in ways that are not possible in any single language out there.

This is because TypeScript is designed to allow you to work seamlessly with a highly dynamic language like JavaScript. Here we cover a few tricks for moving types around in TypeScript.

Key motivation for these : You change one thing and everything else just updates automatically and you get nice errors if something is going to break, like a well designed constraint system.

Copying both the Type + Value

If you want to move a class around you might be tempted to do the following:

  1. class Foo { }
  2. var Bar = Foo;
  3. var bar: Bar; // ERROR: "cannot find name 'Bar'"

This is an error because var only copied the Foo into the variable declaration space and you therefore cannot use Bar as a type annotation. The proper way is to use the import keyword. Note that you can only use the import keyword in such a way if you are using namespaces or modules (more on these later):

  1. namespace importing {
  2. export class Foo { }
  3. }
  4. import Bar = importing.Foo;
  5. var bar: Bar; // Okay

This import trick only works for things that are both type and a variable.

Capturing the type of a variable

You can actually use a variable in a type annotation using the typeof operator. This allows you to tell the compiler that one variable is the same type as another. Here is an example to demonstrate this:

  1. var foo = 123;
  2. var bar: typeof foo; // `bar` has the same type as `foo` (here `number`)
  3. bar = 456; // Okay
  4. bar = '789'; // ERROR: Type `string` is not `assignable` to type `number`

Capturing the type of a class member

Similar to capturing the type of a variable, you just declare a variable purely for type capturing purposes:

  1. class Foo {
  2. foo: number; // some member whose type we want to capture
  3. }
  4. // Purely to capture type
  5. declare let _foo: Foo;
  6. // Same as before
  7. let bar: typeof _foo.foo;

Capturing the type of magic strings

Lots of JavaScript libraries and frameworks work off of raw JavaScript strings. You can use const variables to capture their type e.g.

  1. // Capture both the *type* and *value* of magic string:
  2. const foo = "Hello World";
  3. // Use the captured type:
  4. let bar: typeof foo;
  5. // bar can only ever be assigned to `Hello World`
  6. bar = "Hello World"; // Okay!
  7. bar = "anything else "; // Error!

In this example bar has the literal type "Hello World". We cover this more in the literal type section.

Capturing the name of the keys

The keyof operator lets you capture the key names of a type. E.g. you can use it to capture the key names of a variable by first grabbing its type using typeof:

  1. const colors = {
  2. red: 'red',
  3. blue: 'blue'
  4. }
  5. type Colors = keyof typeof colors;
  6. let color: Colors;
  7. color = 'red'; // okay
  8. color = 'blue'; // okay
  9. color = 'anythingElse'; // Error

This allows you to have stuff like string enums + constants quite easily, as you just saw in the above example.