Inline Object Type Annotations
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 37 of 73.
Sometimes you need to define the structure of an object for a single, specific use case. Instead of creating a reusable type alias or interface, TypeScript allows you to define the object's shape directly where you declare the variable. This is called an inline object type annotation.
The syntax uses curly braces to describe the object's properties and their types right in the variable declaration:
let user: { name: string; id: number } = {
name: "Alice",
id: 123
};This approach is perfect for one-off object structures that you don't plan to reuse elsewhere in your code.
Challenge
EasyCreate a variable named student with an inline object type annotation that defines the following structure:
nameproperty of typestringstudentIdproperty of typenumberisEnrolledproperty of typeboolean
Assign the variable the following values:
name:"Sarah Johnson"studentId:12345isEnrolled:true
Create another variable named course with an inline object type annotation that defines the following structure:
titleproperty of typestringcreditsproperty of typenumberinstructorproperty of typestring
Assign the variable the following values:
title:"Introduction to TypeScript"credits:3instructor:"Dr. Smith"
Print the following information on separate lines:
- The student's name
- The student's ID
- The course title
- The number of credits
Try it yourself
// TODO: Write your code here
// Create the student variable with inline object type annotation
// Create the course variable with inline object type annotation
// Print the required informationThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples6Typing Objects and Interfaces
Inline Object Type AnnotationsType Aliases for ObjectsIntroduction to InterfacesInterfaces vs. Type AliasesOptional & Readonly PropsExtending Interfaces and TypesAdding Methods to InterfacesRecap: Defining Object Shapes