I came across TypeScript from a recently released Generative AI Retrieval-Augmented Generation Toolkit that generates AI Applications using LlamaIndex with Low Code, which simplifies the process of building AI applications through a simple command-line interface.
In this post, I’m going to demonstrate TypeScript with a code example.
YouTube Tutorial
The video that goes through the above in streaming format is below.
TypeScript is a superset of JavaScript that compiles down to plain JavaScript. It introduces optional static typing, which allows developers to specify types for variables, function parameters, and return values. This feature can catch a variety of programming errors at compile time, long before the code is run. This is in contrast to Python and R that are frequently used in data analytics today.
Static vs. Dynamic Type Checking
To fully appreciate TypeScript's capabilities, it's crucial to understand the difference between static and dynamic type checking:
Static Type Checking: TypeScript checks the types of variables at compile time. This process helps catch errors early, reduces the likelihood of type-related bugs in runtime, and improves the developer experience through features like auto-completion and refactoring tools.
Dynamic Type Checking is a method of verifying the types of variables at runtime, as opposed to static type checking, which occurs at compile time.
The below code example demonstrates how TypeScript manages static and dynamic checks with a function designed to store book information in Visual Studio:
Static Type Checking
In the above code example, the ‘Book’ interface explicitly requires ‘title’ and ‘author’ to be strings, and ‘pages’ to be a number. And the ‘displayBookDetails’ function is strictly typed to accept only ‘Book’ objects. When an object is passed to ‘displayBookDetails’ that does not conform to the ‘Book’ interface, TypeScript’s compiler will throw an error, indicating a type mismatch. Notice the above code isn’t run at this point.
Dynamic Type Checking
In the above code example, the ‘processPayment’ function accepts a parameter: ‘amount’ of the type ‘any’, which allows ‘amount’ to be any type at runtime.
Inside the function, a dynamic type check [typeof amount == ‘number’] is performed to ensure that ‘amount’ is actually a number before processing. This is necessary because the functions allows any type to be passed in, unlike the static type check above.
In the case where a string is passed instead of a number, an error message is printed.
You Look Like a Thing and I Love You
People often give AI tasks that are too hard. Sometimes, the programmers only find out here’s a problem when their AI try and fail. Other times, they don’t realize that their AI is solving a different, easier problem than the one they had hoped it would solve (rely on the length of a medical case file rather than its contents to identify problem cases). Still other programmers just pretend that they’ve figured out how to solve the problem with AI while secretly using humans to do it instead.
Udemy Python vs. R course
My Udemy course on learning Python and R side by side is now live for anyone who might be interested in learning these two popular data analytics languages together.






