💻 Programming

Type

Type

Defines the kind of data. Static/dynamic typing. Type safety is important for preventing bugs.

📖 Detailed Explanation

A type is a classification system that defines the kind of data. There are various types such as numbers, strings, booleans, arrays, and objects, and each type determines the operations that can be applied to that data and how it is stored. A type system is a core mechanism that ensures the safety and correctness of a program.

Static typing checks types at compile time. Java, C++, TypeScript, Rust, and others fall into this category, and type errors can be found before execution. Dynamic typing determines types at runtime, and Python, JavaScript, Ruby, and others are representative examples. It is flexible but carries the possibility of runtime errors.

Strong typing restricts implicit type conversion (Python, Java). Weak typing allows automatic type conversion (JavaScript, PHP). "1" + 2 becomes "12" in JavaScript, but raises an error in Python.

Modern languages support type inference, in which the compiler infers types without explicit type declarations. In TypeScript, let x = 5 automatically becomes the number type. Generics make it possible to write reusable code while maintaining type safety.

💻 Code Example

// TypeScript 타입 시스템 예제

// 1. 기본 타입
let count: number = 42;
let name: string = "Alice";
let isActive: boolean = true;
let items: number[] = [1, 2, 3];

// 2. 타입 추론
let inferred = "Hello";  // string으로 추론
// inferred = 123;  // Error: Type 'number' is not assignable to type 'string'

// 3. 유니온 타입
type StringOrNumber = string | number;
function display(value: StringOrNumber) {
    if (typeof value === "string") {
        console.log(value.toUpperCase());
    } else {
        console.log(value.toFixed(2));
    }
}

// 4. 인터페이스와 타입 별칭
interface User {
    id: number;
    name: string;
    email?: string;  // optional
}

type Point = {
    x: number;
    y: number;
};

// 5. 제네릭
function first(arr: T[]): T | undefined {
    return arr[0];
}

const firstNum = first([1, 2, 3]);     // number | undefined
const firstStr = first(["a", "b"]);    // string | undefined

// 6. 타입 가드
function isString(value: unknown): value is string {
    return typeof value === "string";
}

🗣️ Examples of Practical Conversations

Senior: We often get runtime errors because there's no type definition for the API response. Let's add types.

Junior: Wouldn't using any solve it quickly?

Senior: Using any gives up type safety. At the very least, use unknown and narrow it with a type guard, or generate types automatically from the API schema.

Interviewer: What are the pros and cons of static typing and dynamic typing?

Candidate: Static typing catches errors at compile time, so it is safe, and IDE autocompletion is powerful. However, initial development speed can be slow. Dynamic typing allows fast prototyping, but it has runtime errors and difficulty with refactoring.

Interviewer: What is the difference between any and unknown in TypeScript?

Candidate: any ignores all type checking, while unknown requires you to narrow the type before use. unknown is safer, so it is recommended for handling external data.

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: This function's return type is any, so please specify it explicitly.

Author: It was complicated, so I left it to inference.

Reviewer: It's better to specify the return type of a public API explicitly. It prevents unintended type changes when refactoring.

⚠️ Cautions

🔗 Related Terms

📚 Learn More