> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript unknown

The `unknown` type is a type-safe top type in TypeScript's type system. It represents a value that can be of any type, but unlike the `any` type, it enforces strict compiler checks by prohibiting arbitrary operations, property access, or method calls until the value's specific type is resolved.

## Assignability

Because `unknown` is a top type, all other types are assignable to it.

```typescript theme={"dark"}
let value: unknown;

value = 42;           // Valid
value = "string";     // Valid
value = false;        // Valid
value = { a: 1 };     // Valid
value = null;         // Valid
value = undefined;    // Valid
```

Conversely, `unknown` is strictly constrained in outbound assignability. A variable of type `unknown` can only be assigned to variables of type `unknown` or `any`.

```typescript theme={"dark"}
let unknownValue: unknown = "data";

let val1: any = unknownValue;      // Valid
let val2: unknown = unknownValue;  // Valid

// let val3: string = unknownValue; // Error: Type 'unknown' is not assignable to type 'string'
// let val4: object = unknownValue; // Error: Type 'unknown' is not assignable to type 'object'
```

## Operational Constraints

The TypeScript compiler treats `unknown` as a completely opaque value. You cannot perform operations on it, access its properties, or invoke it as a function or constructor.

```typescript theme={"dark"}
let target: unknown = { execute: () => console.log("running") };

// target.execute(); // Error: Object is of type 'unknown'
// target();         // Error: Object is of type 'unknown'
// new target();     // Error: Object is of type 'unknown'
```

## Type Resolution

To interact with an `unknown` value, you must resolve its type using either control flow analysis (type narrowing) or type assertions.

**1. Type Narrowing**
Using type guards (`typeof`, `instanceof`, or custom type guard functions) allows the compiler to narrow the `unknown` type to a more specific type within a conditional block.

```typescript theme={"dark"}
let data: unknown = "TypeScript";

if (typeof data === "string") {
    // Within this block, 'data' is narrowed to 'string'
    console.log(data.toUpperCase()); 
}
```

**2. Type Assertions**
If the type is known to the developer but not the compiler, a type assertion can be used to override the `unknown` constraint.

```typescript theme={"dark"}
let data: unknown = "TypeScript";

// Asserting that 'data' is a string
let length: number = (data as string).length; 
```

## Union and Intersection Mechanics

When combined with other types in unions and intersections, `unknown` follows specific algebraic rules:

**Union Types**
A union of `unknown` and any other type (except `any`) evaluates to `unknown`. It absorbs other types because `unknown` already encompasses all possible values.

```typescript theme={"dark"}
type T1 = unknown | string;  // Resolves to unknown
type T2 = unknown | number;  // Resolves to unknown
type T3 = unknown | any;     // Resolves to any
```

**Intersection Types**
An intersection of `unknown` and any other type evaluates to the other type. Since `unknown` places no structural constraints on a type, intersecting with it does not alter the intersected type.

```typescript theme={"dark"}
type T4 = unknown & string;  // Resolves to string
type T5 = unknown & number;  // Resolves to number
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
