TypeScript Junior interview questions and answers
75 questions for a TypeScript (Junior) interview - with correct answers, explanations and links to the docs.
Take this quiz- 1.What is TypeScript?
- 2.Does TypeScript check types while the program is running?
- 3.How does
unknowndiffer fromany? - 4.What does the
nevertype mean? - 5.What type does TypeScript infer for
let x = 5;? - 6.What's the difference between
interfaceandtypefor describing an object? - 7.How do you declare an optional object property?
- 8.What does the
readonlymodifier do in{ readonly id: number }? - 9.What does the type
string | numbermean? - 10.What does the intersection
type Admin = User & { role: 'admin' }give you? - 11.What is the literal type
'left' | 'right'? - 12.How do you type a function that takes a string and returns a number?
- 13.How do you declare an optional function parameter?
- 14.What is a tuple in TypeScript?
- 15.How do you type an array of strings?
- 16.What is a generic?
- 17.What does the utility type
Partial<User>do? - 18.How do
Pick<User, 'id' | 'name'>andOmit<User, 'password'>differ? - 19.What does
Record<string, number>describe? - 20.What does
keyof { id: number; name: string }return? - 21.What does
typeofdo in a type position:type Config = typeof config? - 22.What's the right way to type a React component's props?
- 23.What is the type of the event in
onChangefor an<input>in React? - 24.How do you specify the state type if the initial value is
null:useState(null)? - 25.What does the type assertion
value as stringdo? - 26.What does the
!operator do inuser!.name? - 27.What does the
strictflag intsconfig.jsonenable? - 28.What does
strictNullChecksgive you? - 29.How do you narrow
string | numbertostringinside a condition? - 30.How does
enum Color { Red, Green }differ fromtype Color = 'red' | 'green'? - 31.What does
as constdo inconst roles = ['admin', 'user'] as const? - 32.What is structural typing in TypeScript?
- 33.What is a
.d.tsfile? - 34.Why install packages like
@types/lodash? - 35.What does
tsccompile a TypeScript file into? - 36.What is the type of
eincatch (e)withstrictenabled? - 37.What does the return type
voidmean? - 38.How do you describe an object with arbitrary string keys and number values using an index signature?
- 39.How do you extend an interface?
- 40.What does
implementsdo in a class? - 41.How does
privatein TypeScript differ from#fieldin JavaScript? - 42.How do you type an async function that returns a user?
- 43.What does TypeScript do with
const el = document.getElementById('app')? - 44.What is a union with a common discriminant field?
- 45.How do you get the return type of the
createStorefunction? - 46.What happens when you pass an object literal with an extra property?
- 47.What is
tsconfig.json? - 48.How do you gradually migrate a JS project to TypeScript?
- 49.How does
Required<T>differ fromPartial<T>? - 50.How do you specify that a function accepts any number of numbers?
- 51.What does the
noImplicitAnyoption report? - 52.How does
(string | number)[]differ fromstring[] | number[]? - 53.What type does
const direction = 'left';get? - 54.What is the type of
users.find((u) => u.id === id)ifusers: User[]? - 55.Why can
typeof value === 'object'be a trap when narrowing? - 56.What does
if (count)miss whencount: number | undefined? - 57.How does
instanceofhelp with types? - 58.Why can't you check
if (data is User)at runtime? - 59.What does
Readonly<User>do? - 60.What does
Record<'draft' | 'published', number>require? - 61.What type does
const items = [];get withstrictenabled? - 62.How do you call a generic function with an explicit type argument?
- 63.How do you write a reusable type for an event handler function?
- 64.Why can
[1, 2].forEach((n) => console.log(n))pass a callback with one parameter whenforEachpasses three? - 65.What's the result type of
user?.address?.cityifcity: string? - 66.What is the type of
const port = env.PORT ?? 3000ifenv.PORT: string | undefined? - 67.What type does
await (await fetch(url)).json()have? - 68.Which type should a React component use for
childrenthat may be text, elements or nothing? - 69.Are
Array<string>andstring[]different types? - 70.How do you check types in CI when a bundler (Vite, esbuild) already compiles TS without type checking?
- 71.How does an optional property
name?: stringdiffer fromname: string | undefined? - 72.How do you describe an object with a method in a type?
- 73.Can recent Node.js versions run a
.tsfile directly? - 74.What type does a function parameter with a default value get?
- 75.Why is
@ts-expect-errorsafer than@ts-ignore?