explain typescript
- User
explain typescript
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
explain typescript
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Let me break down TypeScript in a comprehensive way:
TypeScript is a programming language developed by Microsoft that builds upon JavaScript by adding static type definitions. Think of it as JavaScript with superpowers!
// JavaScript
let name = "John";
name = 42; // This works in JS, but might cause bugs
// TypeScript
let name: string = "John";
name = 42; // Error: Type 'number' is not assignable to type 'string'
// Common types
let isDone: boolean = false;
let decimal: number = 6;
let color: string = "blue";
let list: number[] = [1, 2, 3];
let tuple: [string, number] = ["hello", 10];
interface User {
name: string;
age: number;
email?: string; // Optional property
}
function greetUser(user: User) {
console.log(`Hello, ${user.name}!`);
}
class Animal {
private name: string;
constructor(name: string) {
this.name = name;
}
makeSound(): void {
console.log("Some sound");
}
}
class Dog extends Animal {
makeSound(): void {
console.log("Woof!");
}
}
function getArray<T>(items: T[]): T[] {
return new Array<T>().concat(items);
}
let numArray = getArray<number>([1, 2, 3]);
let strArray = getArray<string>(["hello", "world"]);
function calculateTotal(price: number, quantity: number): number {
return price * quantity;
}
calculateTotal("10", 5); // Error: Argument of type 'string' not assignable to parameter of type 'number'
interface Printable {
print(): void;
}
class Document implements Printable {
print() {
console.log("Printing document...");
}
}
type StringOrNumber = string | number;
function processValue(value: StringOrNumber) {
if (typeof value === "string") {
return value.toUpperCase();
}
return value * 2;
}
// TypeScript can infer types automatically
let message = "Hello"; // TypeScript knows this is a string
let numbers = [1, 2, 3]; // TypeScript knows this is number[]
function log(target: any, key: string) {
console.log(`Accessing property ${key}`);
}
class Example {
@log
name: string;
}
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
interface Todo {
title: string;
description: string;
}
const todo: Readonly<Todo> = {
title: "Learn TypeScript",
description: "Study hard!"
};
// Partial makes all properties optional
type PartialUser = Partial<User>;
// Pick selects specific properties
type NameOnly = Pick<User, 'name'>;
// Omit removes specific properties
type NoEmail = Omit<User, 'email'>;
interface Config {
apiUrl: string;
timeout: number;
}
function initialize(config: Config) {
// Implementation
}
// Don't
const numbers: number[] = [1, 2, 3].map((num: number): number => num * 2);
// Do
const numbers = [1, 2, 3].map(num => num * 2);
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
npm install -g typescript
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
tsc filename.ts
TypeScript is particularly valuable for:
Would you like me to elaborate on any of these aspects or provide more specific examples?