Recap: Introduction to TS
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 5 of 73.
Challenge
EasyCreate a constant named userId with an explicit type annotation of number and assign it the value 12345.
Create a variable named username with an explicit type annotation of string and assign it the value "john_doe".
You are provided with the following line of code that contains a type error:
username = userId;Fix this type error by creating a new variable called userIdString that converts the userId to a string using the String() function, then assign userIdString to username instead.
Finally, print both userId and username to the console on separate lines.
Try it yourself
// TODO: Write your code here
// Create the constant userId with type annotation
// Create the variable username with type annotation
// Fix the type error by converting userId to string
// Assign the converted value to username
// Print the results
console.log(userId);
console.log(username);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 Tuples