---
source: https://iinventedlight.com/library/notes/typescript-type-compatibility.md
title: "Type compatibility and structural typing"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (microsoft/TypeScript-Website); prose restates cited source lines"
attribution: "Summary by iinventedlight (AI-assisted). Sources: TypeScript (Microsoft), TypeScript: Documentation - Type Compatibility, CC BY 4.0 (microsoft/TypeScript-Website); TypeScript (Microsoft), TypeScript: Documentation - Creating Types from Types, CC BY 4.0 (microsoft/TypeScript-Website)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: TypeScript"
tags: ['typescript', 'types', 'structural-typing']
sources:
  - id: S1
    title: "TypeScript: Documentation - Type Compatibility"
    publisher: TypeScript (Microsoft)
    url: https://www.typescriptlang.org/docs/handbook/type-compatibility.html
    accessed: 2026-11-04
    license: "CC BY 4.0 (microsoft/TypeScript-Website)"
  - id: S2
    title: "TypeScript: Documentation - Creating Types from Types"
    publisher: TypeScript (Microsoft)
    url: https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
    accessed: 2026-11-04
    license: "CC BY 4.0 (microsoft/TypeScript-Website)"
---
# Type compatibility and structural typing

**Answer:** Languages like C# and Java use nominal typing instead.

## Example

```ts
interface Pet { name: string }
class Dog { name = "Rex"; bark() {} }
let p: Pet = new Dog(); // OK: Dog has name
```

## Rules of thumb

- Return types: the source must return at least what the target promises.
- Private and protected class members make compatibility depend on the declaring class.

## Building types from types

Because compatibility is about shape, TypeScript offers operators to derive shapes: `keyof`, `typeof`, indexed access, conditional types, mapped types and template literal types.

```ts
type Keys = keyof Pet;                 // "name"
type ReadonlyPet = { readonly [K in keyof Pet]: Pet[K] };
```

## Claims
- id: C1
  claim: Structural typing relates types based solely on their members.
  source: S1
  evidence: "Structural typing is a way of relating types based solely on their members."
- id: C2
  claim: This is in contrast with nominal typing.
  source: S1
  evidence: "This is in contrast with nominal typing."
- id: C3
  claim: In C# or Java the equivalent code would be an error.
  source: S1
  evidence: "In nominally-typed languages like C# or Java, the equivalent code would be an error"
- id: C4
  claim: TypeScript offers mapped types.
  source: S2
  evidence: "Mapped Types"
- id: C5
  claim: TypeScript offers conditional types.
  source: S2
  evidence: "Conditional Types"

## Sources
- [S1] TypeScript: Documentation - Type Compatibility. TypeScript (Microsoft). https://www.typescriptlang.org/docs/handbook/type-compatibility.html . License: CC BY 4.0 (microsoft/TypeScript-Website). Accessed 2026-11-04.
- [S2] TypeScript: Documentation - Creating Types from Types. TypeScript (Microsoft). https://www.typescriptlang.org/docs/handbook/2/types-from-types.html . License: CC BY 4.0 (microsoft/TypeScript-Website). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
