Podcast
Questions and Answers
What keyword is used to create a new name for a type in TypeScript?
What keyword is used to create a new name for a type in TypeScript?
Which of the following is a correct syntax for declaring a tuple type?
Which of the following is a correct syntax for declaring a tuple type?
Which statement about the export
and import
statements is true?
Which statement about the export
and import
statements is true?
What does the readonly
modifier do when applied to an array in TypeScript?
What does the readonly
modifier do when applied to an array in TypeScript?
Signup and view all the answers
What is the purpose of a callback function in TypeScript?
What is the purpose of a callback function in TypeScript?
Signup and view all the answers
Which loop in TypeScript is specifically used to iterate over the keys of an object?
Which loop in TypeScript is specifically used to iterate over the keys of an object?
Signup and view all the answers
When defining an enum, how would you assign string values to its members?
When defining an enum, how would you assign string values to its members?
Signup and view all the answers
How does explicit casting work in TypeScript?
How does explicit casting work in TypeScript?
Signup and view all the answers
Study Notes
TypeScript Basics
Variables and Types
-
Declaration: Use
let
,const
, orvar
for variable declarations. -
Types:
-
Primitive Types:
number
,string
,boolean
,null
,undefined
,symbol
,bigint
. -
Array Types:
- Syntax:
number[]
orArray<number>
.
- Syntax:
-
Tuple Types: Fixed-length array with specified types, e.g.,
[string, number]
.
-
Primitive Types:
Interfaces and Types
-
Interfaces:
- Define the shape of an object.
- Can be extended with
extends
. - Supports optional properties using
?
.
-
Type Aliases:
- Create new names for types using the
type
keyword. - Example:
type Point = { x: number; y: number; }
.
- Create new names for types using the
Functions and Callbacks
-
Function Declaration:
- Syntax:
function funcName(param: type): returnType {}
.
- Syntax:
-
Optional Parameters: Use
?
. - Default Parameters: Define default values.
- Callback Functions: Pass functions as arguments.
- Function Types: Define types for function parameters and return values.
Modules and Namespaces
-
Modules:
- Separate code into files using
export
andimport
. - Can export variables, functions, and classes.
- Separate code into files using
-
Namespaces:
- Group related code.
- Use the
namespace
keyword. - Useful for organizing code without module boundaries.
Loops
- For Loop: Standard loop structure.
- For...of: Iterates over iterable objects (arrays, strings).
- For...in: Iterates over object keys.
- While Loop: Continues until a condition is false.
Arrays
-
Declaration:
let arr: number[] = [1, 2, 3];
. -
Array Methods:
-
push()
,pop()
,map()
,filter()
,reduce()
.
-
-
Readonly Arrays: Use
readonly
modifier to prevent modification.
Enums
-
Enum Definition: Group related constants.
- Syntax:
enum Direction { Up, Down, Left, Right }
.
- Syntax:
-
String Enums: Assign string values to enum members.
- Example:
enum Colors { Red = 'RED', Green = 'GREEN' }
.
- Example:
Explicit Casting
-
Casting Syntax: Use
as
or angle brackets for type assertion.- Example:
let strLength = (someValue as string).length;
.
- Example:
Type Aliasing
-
Creating Type Aliases: Use the
type
keyword to create custom types.- Example:
type ID = string | number;
.
- Example:
Structural Typing
- Definition: Type compatibility based on the structure of the types.
- Duck Typing: If it has the required properties, it is compatible.
- Example: Interfaces can be compatible even if they have different names if their structures match.
Variables and Types
- Utilize
let
,const
, orvar
for variable declarations to define mutable and immutable variables. -
Primitive Types include:
-
number
: Represents numeric values. -
string
: For text values. -
boolean
: Represents true/false values. -
null
andundefined
: Represent empty or uninitialized values. -
symbol
: Unique and immutable values used as object keys. -
bigint
: Represents large integers beyond thenumber
range.
-
-
Array Types are defined using:
- Syntax:
number[]
for an array of numbers, orArray<number>
.
- Syntax:
-
Tuple Types allow specifying fixed-length arrays with designated types, e.g., a tuple can look like
[string, number]
.
Interfaces and Types
-
Interfaces serve to define the structure of objects and can be extended via the
extends
keyword. - They allow optional properties denoted by
?
to specify attributes that may not be present. -
Type Aliases allow creating custom names for types using the
type
keyword, such astype Point = { x: number; y: number; }
.
Functions and Callbacks
-
Function Declarations use the format:
function funcName(param: type): returnType {}
for defining functions. -
Optional Parameters can be indicated using
?
to make parameters non-mandatory. - Default Parameters allow specifying default values in case no arguments are passed.
- Callback Functions enable passing functions as arguments to other functions.
- Function Types specify the types of function parameters and their return values.
Modules and Namespaces
-
Modules help segregate code into files using
export
andimport
keywords for better organization. - Modules can export multiple elements like variables, functions, and classes.
-
Namespaces are useful for grouping related code without the constraints of module boundaries. Use the
namespace
keyword for organization.
Loops
- For Loop is a standard iteration structure to execute a block of code multiple times.
- For...of loop is designed to iterate over iterable objects, such as arrays and strings.
- For...in loop iterates through keys of objects.
- While Loop continues executing as long as a specified condition remains true.
Arrays
-
Array Declaration can be done as:
let arr: number[] = [1, 2, 3];
. - Common Array Methods include:
-
push()
: Adds an item to the end. -
pop()
: Removes the last item. -
map()
,filter()
,reduce()
: Higher-order functions for manipulating arrays.
-
-
Readonly Arrays can be defined using the
readonly
modifier to prevent any modifications.
Enums
-
Enum Definition allows grouping related constants for better readability and structure, e.g.,
enum Direction { Up, Down, Left, Right }
. -
String Enums enable assigning string values to enum members, like
enum Colors { Red = 'RED', Green = 'GREEN' }
.
Explicit Casting
-
Casting Syntax can utilize
as
or angle brackets for type assertion, such aslet strLength = (someValue as string).length;
.
Type Aliasing
-
Creating Type Aliases can be achieved using the
type
keyword for simplifying type definitions, e.g.,type ID = string | number;
.
Structural Typing
- Definition: Compatible types are determined by their structure rather than their explicit definitions.
- Duck Typing indicates a type is compatible if it has the necessary properties required by the context.
- Example: Interfaces can be compatible across different naming if their properties align.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the essential concepts of variables and types in TypeScript. You will learn about variable declarations, primitive types, array types, and tuple types. Test your understanding of interfaces and their role in defining object shapes.