Skip to content

Solutions for Programming Hero Level 2 - Assignment 1 (TypeScript problems and concepts).

Notifications You must be signed in to change notification settings

arifuddincoder/ph-level2-a1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TypeScript Blog

1. What is the use of the keyof keyword in TypeScript? Provide an example.

keyof āĻšāϞ⧋ TypeScript-āĻāϰ āĻāĻ•āϟāĻŋ type operatorāĨ¤
āĻāϟāĻŋ āϕ⧋āύ⧋ object type āĻāϰ āϏāĻŦ keys āĻŦ⧇āϰ āĻ•āϰ⧇ āϤāĻžāĻĻ⧇āϰāϕ⧇ āĻāĻ•āϟāĻž union type āĻŦāĻžāύāĻžā§ŸāĨ¤

📘 Example

type Person = {
  name: string;
  age: number;
  city: string;
};

// keyof Person āĻŽāĻžāύ⧇:
type PersonKeys = keyof Person; 
// => "name" | "age" | "city"

1. Explain the difference between any, unknown, and never types in TypeScript.

  • any → āϝ⧇āϕ⧋āύ⧋ āĻ•āĻŋāϛ⧁ āĻšāϤ⧇ āĻĒāĻžāϰ⧇, āϟāĻžāχāĻĒ-āĻšā§‡āĻ• āύ⧇āχāĨ¤
let x: any = 10;
x = "hello"; // allowed
  • unknown → āύāĻŋāϰāĻžāĻĒāĻĻ any, āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻžāϰ āφāϗ⧇ āϟāĻžāχāĻĒ-āĻšā§‡āĻ• āĻĻāϰāĻ•āĻžāϰāĨ¤
let y: unknown = "world";
if (typeof y === "string") {
  console.log(y.toUpperCase());
}
  • never → āĻāĻŽāύ āĻ•āĻŋāϛ⧁ āϝāĻž āĻ•āĻ–āύ⧋ āϘāϟāĻŦ⧇ āύāĻžāĨ¤
function fail(msg: string): never {
  throw new Error(msg);
}

About

Solutions for Programming Hero Level 2 - Assignment 1 (TypeScript problems and concepts).

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published