TypeScript with React/Node.js Flashcards

Typing React Components & Hooks Typing Express Middleware Type Declarations (@types) Global Types Third-party Package Integration (41 cards)

1
Q

What does it mean to type a React component in TypeScript?

A

It means defining prop and state types using interfaces or type aliases to ensure type safety and better tooling support.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you type React functional components?

A

Use React.FC<Props> or explicitly define props: (props: Props) => JSX.Element.</Props>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you type useState in TypeScript?

A

Specify the type inside useState like: const [value, setValue] = useState<number>(0).</number>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a benefit of typing React hooks?

A

Typing hooks provides compile-time safety, improves IDE autocomplete, and prevents runtime errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you type useEffect’s cleanup function?

A

The cleanup function should return void or undefined; useEffect(() => { return () => { … } }, []).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you type Express middleware?

A

Use the types from ‘express’, e.g., (req: Request, res: Response, next: NextFunction) => void.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is ‘@types/express’?

A

It is a package providing TypeScript type declarations for Express.js, enabling strong typing support.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the role of ‘@types’ packages?

A

They provide external type definitions for JavaScript libraries that do not have native TypeScript support.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are global types in TypeScript?

A

Types that are declared in the global namespace and available throughout the application without imports.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you declare a global type in TypeScript?

A

Use ‘declare global { interface MyType {} }’ in a .d.ts file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a use case for third-party type declarations?

A

Using JavaScript libraries like lodash, express, or moment with type safety in a TypeScript project.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an advantage of using TypeScript with React?

A

It improves maintainability, reduces bugs, and offers better refactoring tools via static typing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a disadvantage of strict typing in Express?

A

It can introduce verbosity and a learning curve, especially with complex middleware chains.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a best practice for typing React props?

A

Define a separate Props interface and use it consistently with components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a best practice for typing Express middleware?

A

Use existing types from ‘express’ rather than writing your own types from scratch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s a common pitfall when using @types packages?

A

Not installing them as devDependencies or using outdated types incompatible with the library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do type declarations impact system design?

A

They promote strong contracts between modules, making codebases more robust and easier to navigate.

18
Q

How do global types affect performance?

A

They don’t affect runtime performance but can slow down compilation or cause naming conflicts.

19
Q

What’s an architectural implication of typed middleware?

A

Clear typing ensures that request/response flows are well-understood and contractually enforced.

20
Q

Can using @types improve fault tolerance?

A

Yes, by catching incorrect assumptions at compile-time rather than runtime.

21
Q

What is a real-world tradeoff with global types?

A

They can simplify usage but lead to accidental conflicts or implicit dependencies across the codebase.

22
Q

What’s a good debugging strategy for type issues in Express?

A

Start by inspecting inferred types, and use IDE tooltips or TypeScript’s –noImplicitAny flag.

23
Q

What is a monitoring implication of typed Express handlers?

A

Well-typed handlers allow structured logging and better observability through typed request data.

24
Q

What is a common interview question about typing React components?

A

How do you enforce prop types in a functional component using TypeScript?

25
What is a gotcha when typing useRef in React?
useRef requires generic typing to avoid 'any', e.g., useRef(null).
26
What’s a challenge in typing third-party packages?
Some packages may lack types entirely, requiring custom declarations or manual typing.
27
What is an example of a typed Express middleware?
const logger: RequestHandler = (req, res, next) => { console.log(req.path); next(); };
28
What’s a disadvantage of custom global types?
They can lead to hidden dependencies and be hard to trace or refactor across large codebases.
29
How do you type optional props in React?
Use the '?' modifier: interface Props { title?: string }.
30
What is the purpose of tsconfig.json in global typing?
tsconfig.json controls which files TypeScript uses, including where to find custom global types.
31
What is a performance implication of many @types packages?
Too many or incorrectly installed types can slow down TypeScript compilation or cause conflicts.
32
What is a use case for custom global types?
Declaring types for environmental variables or custom browser APIs used across many modules.
33
What’s a best practice when typing third-party libraries?
Install matching versions of the library and its @types package to avoid incompatibilities.
34
Why is it important to type Express request bodies?
To ensure expected structure and avoid runtime errors caused by undefined or malformed data.
35
What is a best practice when using global types?
Scope them carefully and use naming conventions to avoid polluting the global namespace.
36
What is the benefit of typing response data in Express?
Improves API consistency and enables client-side consumers to rely on structured data.
37
What is a challenge when integrating third-party packages in TypeScript?
Some packages may not be updated frequently, causing type mismatches or requiring workarounds.
38
How do you write a global type declaration file?
Create a *.d.ts file and use declare global { ... } within it.
39
What is a tradeoff of using inferred types in React hooks?
While concise, it may cause bugs if the inferred type is too broad or incorrect.
40
What is the benefit of typed hooks in shared libraries?
They ensure reusability with type safety, especially when accepting callbacks or context values.
41
What does 'as const' do in TypeScript?
It tells the compiler to infer the narrowest possible literal type, useful in defining immutable values.