TypeScript with React/Node.js Flashcards
Typing React Components & Hooks Typing Express Middleware Type Declarations (@types) Global Types Third-party Package Integration (41 cards)
What does it mean to type a React component in TypeScript?
It means defining prop and state types using interfaces or type aliases to ensure type safety and better tooling support.
How do you type React functional components?
Use React.FC<Props> or explicitly define props: (props: Props) => JSX.Element.</Props>
How do you type useState in TypeScript?
Specify the type inside useState like: const [value, setValue] = useState<number>(0).</number>
What is a benefit of typing React hooks?
Typing hooks provides compile-time safety, improves IDE autocomplete, and prevents runtime errors.
How do you type useEffect’s cleanup function?
The cleanup function should return void or undefined; useEffect(() => { return () => { … } }, []).
How do you type Express middleware?
Use the types from ‘express’, e.g., (req: Request, res: Response, next: NextFunction) => void.
What is ‘@types/express’?
It is a package providing TypeScript type declarations for Express.js, enabling strong typing support.
What is the role of ‘@types’ packages?
They provide external type definitions for JavaScript libraries that do not have native TypeScript support.
What are global types in TypeScript?
Types that are declared in the global namespace and available throughout the application without imports.
How do you declare a global type in TypeScript?
Use ‘declare global { interface MyType {} }’ in a .d.ts file.
What is a use case for third-party type declarations?
Using JavaScript libraries like lodash, express, or moment with type safety in a TypeScript project.
What is an advantage of using TypeScript with React?
It improves maintainability, reduces bugs, and offers better refactoring tools via static typing.
What is a disadvantage of strict typing in Express?
It can introduce verbosity and a learning curve, especially with complex middleware chains.
What is a best practice for typing React props?
Define a separate Props interface and use it consistently with components.
What is a best practice for typing Express middleware?
Use existing types from ‘express’ rather than writing your own types from scratch.
What’s a common pitfall when using @types packages?
Not installing them as devDependencies or using outdated types incompatible with the library.
How do type declarations impact system design?
They promote strong contracts between modules, making codebases more robust and easier to navigate.
How do global types affect performance?
They don’t affect runtime performance but can slow down compilation or cause naming conflicts.
What’s an architectural implication of typed middleware?
Clear typing ensures that request/response flows are well-understood and contractually enforced.
Can using @types improve fault tolerance?
Yes, by catching incorrect assumptions at compile-time rather than runtime.
What is a real-world tradeoff with global types?
They can simplify usage but lead to accidental conflicts or implicit dependencies across the codebase.
What’s a good debugging strategy for type issues in Express?
Start by inspecting inferred types, and use IDE tooltips or TypeScript’s –noImplicitAny flag.
What is a monitoring implication of typed Express handlers?
Well-typed handlers allow structured logging and better observability through typed request data.
What is a common interview question about typing React components?
How do you enforce prop types in a functional component using TypeScript?