menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

Why TypeSc...
source image

Medium

6d

read

90

img
dot

Image Credit: Medium

Why TypeScript Structural Typing Works

  • TypeScript checks types based on the shape of values rather than where and how they were declared.
  • This is known as structural typing, in contrast to nominal typing used in most statically typed languages.
  • TypeScript focuses on the structure of values to ensure compatibility, enabling more flexible coding.
  • It allows for handling extra fields in objects as long as the required structure matches.
  • TypeScript performs type checking by examining object structures property by property, even for nested values.
  • Interfaces in TypeScript serve as structural contracts rather than carrying identity.
  • Structural typing enables easier reuse of functions with varying inputs as long as the shape requirements are met.
  • However, the flexibility of structural typing may lead to security vulnerabilities if not accompanied by appropriate checks.
  • TypeScript's focus on shape comparison can be advantageous for writing versatile functions but requires additional security measures at runtime.
  • Care must be taken with structural typing in scenarios where object shape alone determines access or authorization.

Read Full Article

like

5 Likes

For uninterrupted reading, download the app