049 — #15 Function as Type | TypeScript Basics & Data Types | A Complete TypeScript Course
The lecture closes the basics arc by typing a variable as a function. It starts from JavaScript reality: `let greet = greetUser` works, but the inferred `any` type also permits `greet = 100`, and the resulting `greet()` call fails only at runtime ("greet is not a function") with no compile-time complaint. Step one of the fix — `let greet: Function` — restores a compile error on the number assignme
Video: 049 · #15 Function as Type | TypeScript Basics & Data Types | A Complete TypeScript Course · watch · ~11:51 · procademy
Source: this is a written companion to the video. Part of the Favorite TypeScript Videos curated path.
The lecture closes the basics arc by typing a variable as a function. It starts from
JavaScript reality: let greet = greetUser works, but assigning greet = 100
afterwards draws no compile-time complaint in the demo, and the resulting greet()
call fails only at runtime (“greet is not a function”). (Strictly speaking, let greet = greetUser infers the function type rather than any — the lecture treats the
variable as unchecked to motivate the annotation.) Step one of the fix — let greet: Function — restores a compile error on the number assignment but still accepts any
function, demonstrated by assigning a mismatched sum(num1, num2) and getting a silent
wrong result (object-plus-undefined concatenation logged as if fine). Step two is
the real fix: a function-signature annotation, (user: User) => void, so only
functions taking a User and returning nothing are assignable — greetUser and the
new isEligible(user) pass, sum errors on assignment.
Functions are assignable — and any lets anything through
function greetUser(user: User) {
console.log("Hello " + user.name);
}
let greet = greetUser;
greet(user); // Hello John
greet = 100; // no complaint in the demo...
greet(user); // ...runtime error: greet is not a function
The diagnosis ([00:02:10]–[00:02:32]): a variable whose type is any accepts “a string
value, a number value, an object, array, etc.” The 100 assignment compiles; only
running the program surfaces the failure ([00:02:58]–[00:03:09]).
Function type — necessary but too loose
let greet: Function = greetUser;
greet = 100; // compile error now — only functions assignable
This kills the number bug ([00:04:05]–[00:04:30]), but the instructor immediately shows
the hole: sum(num1: number, num2: number) also assigns cleanly, and calling it with
a user object compiles, runs, and logs string-concatenated garbage instead of an
error ([00:05:24]–[00:07:24]).
Function-signature type — the precise fix
let greet: (user: User) => void;
greet = greetUser; // fine — (user: User) => void
greet = sum; // error — signature does not match
Only functions with the given signature can be assigned to greet ([00:08:02]–[00:08:42]).
The parameter name is free; its type (User) and the return type (void) are what
must match.
Proof by second assignee — isEligible
A new isEligible(user: User): void that logs whether user.age >= 18 assigns to
greet without error and runs through the same call site — confirming the signature,
not the function identity, is the gate. The mismatched sum assignment stays red and
is commented out.

Transcript corrections applied above: greetUser (function), greet (variable) (not greet user, discrete, grid, greed, great, strict (as variable)); sum(num1, num2) parameters
(not sum is taking two parameters 1 ... and 2); curly braces (return-type position in signature)
(not curly bases); void (not wide, white); app.ts (not f.ts, .ps file); isEligible
logs/returns per age >= 18 demo (not is eligible returns “true”).
Prev: 048 — TypeScript Type Narrowing Simplified · Next: 050 — #16 Function Type for Callback | TypeScript Basics & Data Types | A Complete TypeScript Course