# Optional & Readonly Properties — TypeScript

Source: https://www.geekswithgeeks.com/en/typescript/ts-optional-readonly-props

> Mark properties as not-always-present, or as never-changing.

## Optional properties

A `?` after the property name means it may be missing from the object entirely.

```typescript
interface Config {
  host: string;
  port?: number;   // optional
}

const c1: Config = { host: "localhost" };        // OK
const c2: Config = { host: "localhost", port: 8080 }; // OK
```

## readonly properties

`readonly` allows setting a property once (usually at creation) but blocks any later reassignment.

```typescript
interface Point {
  readonly x: number;
  readonly y: number;
}

const p: Point = { x: 0, y: 0 };
p.x = 10;
// Error: Cannot assign to 'x' because it is a read-only property.
```

## readonly is compile-time only

`readonly` is erased at compile time — it protects you in TS code, not against JS code that ignores types at runtime.
