QA
Beginner LevelJavaScript & TypeScript

What are basic TypeScript types (primitive, tuple, enum, any, unknown, void, never)?

❓ Interview Prompt: Compare any vs unknown, void vs never, and basic TypeScript primitives.

TypeScript adds explicit annotations for JavaScript primitives plus special types:

Special Type Distinctions:

•
`any`: Disables type checking completely. Allows any operation without safety.
•
`unknown`: Type-safe counterpart of any. Must perform type checking/narrowing before operating on the value!
•
`void`: Indicates a function does NOT return a value.
•
`never`: Indicates a function never returns (e.g. throws an infinite loop or unconditional exception).
•
`Tuple`: Fixed-length array with specific element types at specified indices ([string, number]).

Code Implementation Example

typescript
// Tuple: fixed length and types
let pair: [string, number] = ['Age', 30];

// unknown vs any
let unk: unknown = 'Hello World';
// unk.toUpperCase(); // ❌ Compile Error! Type 'unknown' must be narrowed first.

if (typeof unk === 'string') {
  console.log(unk.toUpperCase()); // ✅ Allowed after type narrowing!
}

// void function
function logMessage(msg: string): void {
  console.log(msg);
}

// never function (never finishes normally)
function throwError(reason: string): never {
  throw new Error(reason);
}
💡Tuple declaration, unknown safety requirements vs any, and void vs never returns.
🎯 Interview Key Takeaway

Prefer unknown over any for untyped data. Use never for unreachable execution paths or throwing functions.

#TypeScript#Types#unknown#Beginner
Back to all JavaScript & TypeScript Questions→

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?