Browse Source

4.3: type annotations in functions. noImplicitAny option.

Frederic G. MARAND 5 years ago
parent
commit
9a3213df7f
2 changed files with 21 additions and 1 deletions
  1. 20 0
      app/app.ts
  2. 1 1
      tsconfig.base.json

+ 20 - 0
app/app.ts

@@ -28,3 +28,23 @@ let value: any = 42;
 let fixedString1: string = (<number>value).toFixed(2);
 let fixedString2: string = (value as number).toFixed(2);
 console.log(value, fixedString1, fixedString2);
+
+/**
+ * y is now optional, while x is not.
+ */
+function foo(x: number, y?: string): string {
+  return x.constructor.toString() + y;
+}
+
+foo(2, "3");
+foo(2);
+
+function bar(...args: number[]) {
+  console.log(args.reduce((accu: number, curr: number) => accu + curr, 0));
+}
+
+bar(1, 2, 3, 4, 5); // (5+6)/2 = 15
+
+// Rejected because of noImplicitAny.
+// function qux(x, y) { return x + y; }
+// qux('a', 'b');

+ 1 - 1
tsconfig.base.json

@@ -9,7 +9,7 @@
     // "watch": true,
 
     /* Strict Type-Checking Options */
-    "noImplicitAny": false,
+    "noImplicitAny": true,
     "strictNullChecks": true,
 
     /* Additional Checks */