惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
U
Unit 42
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
IT之家
IT之家
MyScale Blog
MyScale Blog
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
InfoQ
博客园 - 司徒正美

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Introduction to TypeScript. Special TypeScript data types
Julia Shlykova · 2026-06-16 · via DEV Community

Julia Shlykova

Arrays and tuples

  • array - collection of items (similar to JavaScript). In TypeScript we can specify what value types we expect for items to be:
let arr: number[] = [];

arr[0] = 1;

arr[0] += "string"; // Type 'string' is not assignable to type 'number'

we can declare nested arrays like this: let arr: string[][] = [["string1"], ["string2"], []].

  • tuples - fixed length array that has predefined types for each position:
let tup: [number, string] = [1, "text"];

tup[0] = 2;
tup[1] = 3; //Type 'number' is not assignable to type 'string'

We also can use nested tuples:

const coords: [number, number[]][] = [
  [0, [0, 0]],
  [10, [0, 1]]
];

coords[20] = [2, []];


Literal types

literal types - constraints on types (string, number or boolean), specifying exactly which values the variable is allowed to take:

let status: 'offline' | 'online';

status = 'ofline'; // Type '"ofline"' is not assignable to type '"offline" | "online"'. Did you mean '"offline"'?

Object literal

We can also specify what properties can our object have using object literals:

const obj: {
  a: number,
  b: string,
  c?: boolean
} = {
  a: 1,
  b: 'text',
  c: true
};

obj.b = 2; //Type 'number' is not assignable to type 'string'

We used optional parameter (?:), that allows us to omit value assignment to the property.

enum type

enum (from enumeration) - a collection of named constants linked with an integer (by default enumeration starts from 0):

enum Color {
  Red,
  Green,
  Blue
}

let colorName: string = Color[1];

console.log(colorName);

enum Colors {
  Red = 2,
  Green,
  Blue
}

let colorName: string = Colors[3];

console.log(colorName); // Green

String enums

String enums allow us to create a set of constants mapped to strings:

enum Notification {
  Warning = "This is a warning",
  Success = "This is a success",
  Danger = "This is a danger"
}

console.log(Notification.Danger); // This is a danger

enum as data type of a variable

enum Status {
  Offline = 'OFFLINE',
  Online = 'ONLINE'
}

let userStatus: Status;

userStatus = Status.Offline;

console.log(userStatus); // OFFLINE

enum vs object

You may ask yourself, why would we use enum, if we can achieve the same behavior with object?

Let's look at the last example: we have userStatus type as enum, but in the end of the day it's just a string and let's try to rewrite it with object:

const Status = {
  Offline: 'OFFLINE',
  Online: 'ONLINE'
}

let userStatus: string = Status.Offline;

userStatus = "plain text"; // OK

We can assign another value to userStatus!

Actually the compiler transforms enum to javascript the object. We can even access its properties:

enum Status {
  Offline = 'OFFLINE',
  Online = 'ONLINE'
}

function getAttr(obj: {Offline: string}) {
    return obj.Offline;
}

console.log(getAttr(Status)); // OFFLINE

Actually, we can make enums disappear at the compile time using const. They won't be compiled to the object and the variables assigned to their keys will be assigned to literals:

const enum Status {
  Offline = 'OFFLINE',
  Online = 'ONLINE'
}

let i: Status = Status.Offline;

function getAttr(obj: {Offline: string}) {
    return obj.Offline;
}

console.log(getAttr(Status)); // 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.


Any, Unknown and Type Casts

  • any - this type tells typescript compiler to ignore the variable while type checking. It means that we can assign any value to the variable and call any method on it:
let x: any;

x =1;

x=[2,3];

x(); // compiler allows it, since it doesn't know what type of the variable it is.

any is used in a difficult situation, when we are not able to predict the type. Usually in the case of unceratainty you should use unknown type.

  • unknown - we don't know what the type is going to be but before performing any operations with the variable we have to check its type:
let a: unknown = 10;

if (typeof a === "number") {
  a+=2;
}

console.log(a);

Another way to work with unknown type is to use type casting:

let a: unknown = 10;

// We tell the compiler to treat the variable as its type is number:
let b = (a as number) + 2;

console.log(b);