Functions
Lesson 1 of 1
  1. 1
    When we declare a function in JavaScript, we often expect it to be invoked with arguments of a certain type. JavaScript does not share our expectations: its type flexibility often allows functions…
  2. 2
    In TypeScript, function parameters may be given type annotations with the same syntax as variable declarations: a colon next to the name. The type annotations ensure that the parameters are of the …
  3. 3
    TypeScript normally gives an error if we don’t provide a value for all arguments in a function. This isn’t always desirable; sometimes we’d like to skip providing values. function greet(name: stri…
  4. 4
    If a parameter is assigned a default value, TypeScript will infer the variable type to be the same as the default value’s type. (This is similar to how TypeScript infers the type of an initialized…
  5. 5
    TypeScript can also infer the types of values returned by functions. It does this by looking at the types of the values after a function’s return statements. function createGreeting(name: string) …
  6. 6
    If we’d like to be explicit about what type a function returns, we can add an explicit type annotation after its closing parenthesis. Here, we use the same syntax as other type annotations, a colon…
  7. 7
    By now, we’ve made a pretty convincing case that type annotations are very useful and should always be used unless there’s a very good reason not to. They make everything neat and aid in understand…
  8. 8
    TypeScript recognizes JavaScript comment syntax: // This is a single line comment /* This is a multiline comment */ But it’s common in TypeScript to see a third comment style: documentation com…
  9. 9
    We learned all about how to use TypeScript to specify the types of function parameters and return types. Now we know how to: * Give type annotations to function parameters. * Deal with type annot…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo