TypeScript Fundamentals Flashcards

What is TypeScript? tsconfig.json Basic types (string, number, boolean) let, const, and variable declarations Type annotations and inference Basic compilation and CLI usage (56 cards)

1
Q

What is TypeScript?

A

TypeScript is a strongly typed superset of JavaScript that compiles to plain JavaScript.

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

What is the purpose of tsconfig.json?

A

tsconfig.json is a configuration file that specifies the root files and compiler options required to compile a TypeScript project.

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

What are basic types in TypeScript?

A

Basic types include string, number, boolean, null, undefined, void, and any.

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

What is the difference between let and const in TypeScript?

A

let allows reassignment of variables, while const creates read-only references that cannot be reassigned.

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

What is type annotation in TypeScript?

A

Type annotation is the explicit declaration of a variable’s type, e.g., let age: number = 25;

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

What is type inference in TypeScript?

A

TypeScript automatically infers the type of a variable based on its assigned value if no annotation is provided.

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

How do you compile TypeScript code?

A

Use the command ‘tsc’ (TypeScript Compiler) in the terminal to compile .ts files to .js files.

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

What is the CLI command to initialize a TypeScript project?

A

tsc –init creates a tsconfig.json file with default configuration settings.

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

What are the advantages of using TypeScript?

A

Improved code quality, better tooling support, early bug detection, and enhanced developer productivity.

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

What are the disadvantages of TypeScript?

A

Steeper learning curve, longer setup time, and sometimes verbose syntax compared to plain JavaScript.

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

What is a best practice when using TypeScript types?

A

Prefer explicit type annotations for function parameters and return types to improve readability and type safety.

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

What is a common use case for TypeScript?

A

Large-scale applications where strong typing and IDE support can significantly improve maintainability and refactoring.

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

How does TypeScript impact system design?

A

It enforces contracts via types, improving modularity, code correctness, and long-term maintainability of large systems.

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

What is an example of using a string type?

A

let name: string = ‘Alice’;

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

What is an architectural implication of using TypeScript?

A

TypeScript encourages cleaner module boundaries and makes refactoring safer across large codebases.

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

How does TypeScript affect performance?

A

TypeScript adds no runtime overhead since it compiles to JavaScript; performance is mostly unaffected.

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

Can TypeScript help with fault tolerance?

A

Yes, TypeScript helps catch type-related errors at compile-time, reducing runtime failures.

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

What is a debugging benefit of TypeScript?

A

TypeScript’s strict typing and error messages help identify issues earlier in the development cycle.

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

What’s a real-world tradeoff of using TypeScript?

A

You gain safer, maintainable code at the cost of additional complexity and a compile step in your build process.

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

What’s a common interview question about TypeScript?

A

What are the differences between TypeScript and JavaScript, and when would you use TypeScript?

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

What is a potential gotcha with type inference?

A

If the inferred type is too broad (like ‘any’), it can silently allow bugs that strict typing would have caught.

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

What does ‘strict’ mode in tsconfig.json do?

A

It enables all strict type-checking options, making the compiler more rigorous and reducing bugs.

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

What’s the difference between ‘any’ and ‘unknown’?

A

‘any’ disables type checking, while ‘unknown’ is safer and forces type checking before usage.

24
Q

What happens if you omit a variable’s type in TypeScript?

A

The compiler infers the type from the value assigned, or uses ‘any’ if there’s no context.

25
What’s the benefit of using const for variables?
const ensures that variables cannot be reassigned, making code more predictable and reducing bugs.
26
What is a common pitfall when using 'any'?
Using 'any' too frequently defeats the purpose of TypeScript and can lead to runtime errors.
27
Why is type inference useful?
It allows cleaner code while still benefiting from type checking where the compiler can determine the type.
28
What is a basic example of a function with type annotations?
function greet(name: string): string { return `Hello, ${name}`; }
29
What’s a best practice for organizing tsconfig.json?
Use extends for shared base config, and override specifics in individual projects or packages.
30
How does tsconfig.json help with project scalability?
It centralizes compiler settings, enabling consistent builds and easier configuration across large codebases.
31
What are composite projects in tsconfig.json?
Composite projects support project references, enabling faster builds and modular monorepo structures.
32
What’s a potential issue with using JavaScript libraries in TypeScript?
If type definitions aren’t available, you may need to install @types packages or manually define them.
33
What is the use of 'include' and 'exclude' in tsconfig.json?
They define which files should be compiled or ignored by the TypeScript compiler.
34
What’s the role of 'outDir' in tsconfig.json?
It specifies the output directory for compiled JavaScript files.
35
What’s a best practice for variable declarations?
Use const by default, let when reassignment is necessary, and avoid var altogether.
36
What does the TypeScript compiler do?
It checks type correctness and converts TypeScript code into JavaScript code.
37
What’s a potential gotcha with using 'const'?
'const' prevents reassignment, but the values inside objects or arrays declared with const can still be mutated.
38
Can you use JavaScript files in a TypeScript project?
Yes, by enabling 'allowJs' in tsconfig.json, but you lose type safety unless you add annotations or JSDoc.
39
What’s a good way to transition from JavaScript to TypeScript?
Start by renaming .js files to .ts and gradually add types and stricter compiler settings.
40
What is an example of number type usage?
let age: number = 30;
41
What is the default type if you don’t specify or assign a value?
The type defaults to 'any', which disables type checking.
42
What’s the purpose of 'noImplicitAny' in tsconfig.json?
It prevents variables from implicitly having the 'any' type, forcing explicit typing or inference.
43
How can you enable stricter type checking across a project?
Set 'strict': true in tsconfig.json to enable all strict mode checks.
44
What’s the tradeoff of using strict mode?
It increases safety and code clarity but may require more boilerplate and upfront type definitions.
45
Why is it important to avoid the 'any' type?
It disables type checking and can lead to runtime errors, negating the benefits of TypeScript.
46
What’s a common interview question about tsconfig.json?
What are some important compiler options in tsconfig.json and how do they impact your build process?
47
What is the TypeScript compiler CLI command for a single file?
tsc filename.ts
48
What is the benefit of static typing in large codebases?
It enables better tooling, easier refactoring, and earlier error detection.
49
what is tsc
tsc is the command line tool for the TypeScript compiler. It compiles TypeScript code into JavaScript code, making it compatible with the browser or any JavaScript runtime environment. You can use the tsc command to compile your TypeScript code by running the following command in your terminal or command prompt:
50
what is ts-node
ts-node is a TypeScript execution engine and REPL for Node.js. It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling. This is accomplished by hooking node's module loading APIs, enabling it to be used seamlessly alongside other Node.js tools and libraries.
51
TypeScript vs JavaScript
TypeScript is a superset of JavaScript that adds optional type annotations and other features such as interfaces, classes, and namespaces. JavaScript is a dynamically-typed language that is primarily used for client-side web development and can also be used for server-side development. Here are a few key differences between TypeScript and JavaScript: Types: TypeScript has optional type annotations while JavaScript is dynamically-typed. This means that in TypeScript, you can specify the data type of variables, parameters, and return values, which can help catch type-related errors at compile-time. Syntax: TypeScript extends JavaScript syntax with features like interfaces, classes, and namespaces. This provides a more robust and organized structure for large-scale projects. Tooling: TypeScript has better tooling support, such as better editor integration, type checking, and code refactoring. Backwards Compatibility: TypeScript is fully compatible with existing JavaScript code, which means you can use TypeScript in any JavaScript environment.
52
Unknown in typescript
unknown is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn’t assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type. function f1(a: any) { a.b(); // OK } function f2(a: unknown) { // Error: Property 'b' does not exist on type 'unknown'. a.b(); }
53
Any in typescript
TypeScript has a special type, any, that you can use whenever you don’t want a particular value to cause typechecking errors. When a value is of type any, you can access any properties of it (which will in turn be of type any), call it like a function, assign it to (or from) a value of any type, or pretty much anything else that’s syntactically legal
54
Never in typescript
The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns. Variables also acquire the type never when narrowed by any type guards that can never be true. The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). Even any isn’t assignable to never. Examples of functions returning never: // Function returning never must not have a reachable end point function error(message: string): never { throw new Error(message); }
55
Interface in typescript
TypeScript allows you to specifically type an object using an interface that can be reused by multiple objects. interface Person { name: string; age: number; } function greet(person: Person) { return 'Hello ' + person.name; }
56
class