How it decides

For every position the tests passed through, apply either writes the type it saw or leaves the position alone, and says why. Each section below is one rule it decides by, with small files that end there: run, applied, and the code that made the call. How good the types it writes are, in real projects, is on In real code.

The examples are not written by hand: each is run through the same applier as the playground when the page is built, and a test checks that it still ends in its rule.

writtentype-errorno-informationout-of-scopecontradictionredundantkept-existingalready-annotatedignoredleft-to-typescriptno-site
Parameter●●●●●●●●●●●●●●●
Return type●●●●●●●●●●●●●
Variable●●●●●●●●●●●●●●●●●●
Class field●●●●●●●●●●●
Callback●●●●●●●●●●
this●

Each ● is a rule that ends there, for that kind of position. It links to the rule.

From the code as written

Asked to stay out

syntax.ignored · decided in cst-site-index.ts buildCstSiteIndex

A @ts-capture-ignore comment above a declaration keeps apply away from all of it.

An ignored function's callback

Callback handler Not written: ignored

Before
// @ts-capture-ignore
function on(handler) {
  return handler();
}

on(() => 1);
After ts-capture apply
// @ts-capture-ignore
function on(handler) {
  return handler();
}

on(() => 1);

A @ts-capture-ignore comment above a declaration keeps apply away from all of it.

Observed
() => number
See
the corpus file · open in the playground

An ignored field

Class field settings Not written: ignored

Before
class Panel {
  // @ts-capture-ignore
  settings = JSON.parse('{"dark": true}');
}

new Panel();
After ts-capture apply
class Panel {
  // @ts-capture-ignore
  settings = JSON.parse('{"dark": true}');
}

new Panel();

A @ts-capture-ignore comment above a declaration keeps apply away from all of it.

Observed
{ dark: boolean }
See
the corpus file · open in the playground

An ignored function's parameter

Parameter input Not written: ignored

Before
// @ts-capture-ignore
function parse(input) {
  return JSON.parse(input);
}

parse('{"a":1}');
After ts-capture apply
// @ts-capture-ignore
function parse(input) {
  return JSON.parse(input);
}

parse('{"a":1}');

A @ts-capture-ignore comment above a declaration keeps apply away from all of it.

Observed
string
See
the corpus file · open in the playground

An ignored function's return

Return type Not written: ignored

Before
// @ts-capture-ignore
function load(text) {
  return JSON.parse(text);
}

load('{"a":1}');
After ts-capture apply
// @ts-capture-ignore
function load(text) {
  return JSON.parse(text);
}

load('{"a":1}');

A @ts-capture-ignore comment above a declaration keeps apply away from all of it.

Observed
{ a: number }
See
the corpus file · open in the playground

An ignored variable

Variable settings Not written: ignored

Before
// @ts-capture-ignore
const settings = JSON.parse('{"dark": true}');
After ts-capture apply
// @ts-capture-ignore
const settings = JSON.parse('{"dark": true}');

A @ts-capture-ignore comment above a declaration keeps apply away from all of it.

Observed
{ dark: boolean }
See
the corpus file · open in the playground

A type is written already

syntax.already-typed · decided in cst-routing.ts routeEntries

Apply never replaces a type that is there, whether a person wrote it or an earlier apply did.

A callback typed by the first apply

Callback clock Not written: already-annotated

Run
function stamp(clock) {
  clock();
}

stamp(() => new Date());
After the first apply
function stamp(clock: () => Date) {
  clock();
}

stamp(() => new Date());
After ts-capture apply
function stamp(clock: () => Date) {
  clock();
}

stamp(() => new Date());

The same observations applied a second time meet the type the first apply wrote. Apply never replaces a type that is there, its own or anyone else's.

Observed
() => Date
See
the corpus file

A field typed by the first apply

Class field items Not written: already-annotated

Run
class Store {
  items = JSON.parse("[1, 2]");
}

new Store();
After the first apply
class Store {
  items: number[] = JSON.parse("[1, 2]");
}

new Store();
After ts-capture apply
class Store {
  items: number[] = JSON.parse("[1, 2]");
}

new Store();

The same observations applied a second time meet the type the first apply wrote. Apply never replaces a type that is there, its own or anyone else's.

Observed
number[]
See
the corpus file

A parameter typed by the first apply

Parameter user Not written: already-annotated

Run
function greet(user) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });
After the first apply
function greet(user: { name: string }) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });
After ts-capture apply
function greet(user: { name: string }) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });

The same observations applied a second time meet the type the first apply wrote. Apply never replaces a type that is there, its own or anyone else's.

Observed
{ name: string }
See
the corpus file

A return typed by the first apply

Return type Not written: already-annotated

Run
function load() {
  return JSON.parse('{"a": 1}');
}

load();
After the first apply
function load(): { a: number } {
  return JSON.parse('{"a": 1}');
}

load();
After ts-capture apply
function load(): { a: number } {
  return JSON.parse('{"a": 1}');
}

load();

The same observations applied a second time meet the type the first apply wrote. Apply never replaces a type that is there, its own or anyone else's.

Observed
{ a: number }
See
the corpus file

A variable typed by the first apply

Variable data Not written: already-annotated

Run
const data = JSON.parse('{"id": 1}');
After the first apply
const data: { id: number } = JSON.parse('{"id": 1}');
After ts-capture apply
const data: { id: number } = JSON.parse('{"id": 1}');

The same observations applied a second time meet the type the first apply wrote. Apply never replaces a type that is there, its own or anyone else's.

Observed
{ id: number }
See
the corpus file

No place to write it

syntax.no-site · decided in cst-routing.ts routeEntries

The recorded position is not one a type can go at, as when the code changed after the run. Apply writes nothing there and says so.

A callback parameter moved down a line

Callback clock Not written: no-site

Run
function stamp(clock) {
  clock();
}

stamp(() => new Date());
Changed before apply
// A line added after the run moves everything below it.
function stamp(clock) {
  clock();
}

stamp(() => new Date());
After ts-capture apply
// A line added after the run moves everything below it.
function stamp(clock) {
  clock();
}

stamp(() => new Date());

A line was added above the code after the run. Every recorded position now points one line too early, at no parameter, so apply writes nothing there and says so.

Observed
() => Date
See
the corpus file

A field moved down a line

Class field items Not written: no-site

Run
class Store {
  items = JSON.parse("[1, 2]");
}

new Store();
Changed before apply
// A line added after the run moves everything below it.
class Store {
  items = JSON.parse("[1, 2]");
}

new Store();
After ts-capture apply
// A line added after the run moves everything below it.
class Store {
  items = JSON.parse("[1, 2]");
}

new Store();

A line was added above the code after the run. Every recorded position now points one line too early, at no field, so apply writes nothing there and says so.

Observed
number[]
See
the corpus file

A parameter moved down a line

Parameter user Not written: no-site

Run
function greet(user) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });
Changed before apply
// A line added after the run moves everything below it.
function greet(user) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });
After ts-capture apply
// A line added after the run moves everything below it.
function greet(user) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });

A line was added above the code after the run. Every recorded position now points one line too early, at no parameter, so apply writes nothing there and says so.

Observed
{ name: string }
See
the corpus file

A function moved down a line

Return type Not written: no-site

Run
function load() {
  return JSON.parse('{"a": 1}');
}

load();
Changed before apply
// A line added after the run moves everything below it.
function load() {
  return JSON.parse('{"a": 1}');
}

load();
After ts-capture apply
// A line added after the run moves everything below it.
function load() {
  return JSON.parse('{"a": 1}');
}

load();

A line was added above the code after the run. Every recorded position now points one line too early, at no function, so apply writes nothing there and says so.

Observed
{ a: number }
See
the corpus file

A variable moved down a line

Variable data Not written: no-site

Run
const data = JSON.parse('{"id": 1}');
Changed before apply
// A line added after the run moves everything below it.
const data = JSON.parse('{"id": 1}');
After ts-capture apply
// A line added after the run moves everything below it.
const data = JSON.parse('{"id": 1}');

A line was added above the code after the run. Every recorded position now points one line too early, at no variable, so apply writes nothing there and says so.

Observed
{ id: number }
See
the corpus file

The type belongs somewhere else

syntax.outer-conflict · decided in annotation-eligibility.ts decideVarDeclSite

A variable that holds a function, sits in a generic function, or is initialized by a call that names its type arguments gets its type from there. A type written on the variable would repeat it, or contradict it.

A function's type is written on the function

Variable double Not written: kept-existing

Before
const double = (n) => n * 2;
double(2);
After ts-capture apply
const double = (n: number) => n * 2;
double(2);

The variable holds a function. Its parameters and return are typed where they stand, inside the function, and the variable gets its type from them.

Observed
(n: number) => number · the initializer is a function with its own types
See
the corpus file · open in the playground

Inside a generic function

Variable boxed Not written: kept-existing

Before
function wrap<T>(value: T) {
  const boxed = { value };
  return boxed;
}

wrap(1);
After ts-capture apply
function wrap<T>(value: T) {
  const boxed = { value };
  return boxed;
}

wrap(1);

The run saw one call, with a number. Inside the function the value is a T, which stands for every type a caller can pass, so writing number would be wrong for the others.

Observed
{ value: number } · inside a generic function
See
the corpus file · open in the playground

The call already names its type

Variable ids Not written: kept-existing

Before
const ids = Array.of<number>(1, 2);
After ts-capture apply
const ids = Array.of<number>(1, 2);

The initializer passes <number>, which says what the variable is. Writing the type again would only repeat it, or contradict it.

Observed
number[] · the initializer passes type arguments
See
the corpus file · open in the playground

Only one branch was seen

syntax.one-branch-seen · decided in annotation-eligibility.ts decideVarDeclSite

The initializer can give either side of ??, ||, ?: or ?., and the run saw one. Writing what it saw would forbid the other, so the type is left to TypeScript.

A field where ?? only gave its left side

Class field label Not written: left-to-typescript

Before
const labels = ["a", "b"];

class Badge {
  label = labels.find((l) => l === "a") ?? "none";
}

new Badge();
After ts-capture apply
const labels = ["a", "b"];

class Badge {
  label = labels.find((l) => l === "a") ?? "none";
}

new Badge();

The initializer can give either side of ??, and the run only ever saw one. Writing what it saw would forbid the other, so the type is left to TypeScript.

Observed
string · one branch of the initializer observed
See
the corpus file · open in the playground

A variable where ?? only gave its left side

Variable label Not written: left-to-typescript

Before
const words = ["a", "b"];
const label = words.find((w) => w === "a") ?? "none";
After ts-capture apply
const words = ["a", "b"];
const label = words.find((w) => w === "a") ?? "none";

The initializer can give either side of ??, and the run only ever saw one. Writing what it saw would forbid the other, so the type is left to TypeScript, which knows both.

Observed
string · one branch of the initializer observed
See
the corpus file · open in the playground

Only undefined was seen

syntax.only-undefined-seen · decided in annotation-eligibility.ts decideVarDeclSite

A call or lookup that returned undefined every time the run passed it. TypeScript knows it can return more, so : undefined would forbid the rest.

A Map lookup that missed

Variable missing Not written: left-to-typescript

Before
const prices = new Map([["tea", 3]]);
const missing = prices.get("coffee");
After ts-capture apply
const prices = new Map([["tea", 3]]);
const missing = prices.get("coffee");

The lookup missed, so the run saw undefined alone. TypeScript knows the lookup can also hit. Writing undefined would forbid that, so the type is left to TypeScript.

Observed
undefined · only `undefined` observed
See
the corpus file · open in the playground

The author cast the value

syntax.as-cast · decided in cst-replacements.ts buildCstReplacements

The initializer has an as cast. The cast says what the author wants the value to be, and apply defers to it.

JSON.parse cast as number

Variable port Not written: kept-existing

Before
const port = JSON.parse("3000") as number;
After ts-capture apply
const port = JSON.parse("3000") as number;

The initializer says as number. The cast is what the author asked for, so apply writes nothing over it, whatever the run saw.

Observed
number · the initializer has an `as` cast
See
the corpus file · open in the playground

as const asks for the narrow type

syntax.as-const · decided in initializer-inference.ts hasConstAssertion

An as const initializer is a narrow, readonly type the author asked for. What the run saw can only be wider.

An array as const

Variable sizes Not written: kept-existing

Before
const sizes = ["s", "m"] as const;
After ts-capture apply
const sizes = ["s", "m"] as const;

as const makes the array a readonly tuple of its literals. What the run saw can only be wider, so writing it would undo what the author asked for.

Observed
string[] · the initializer is `as const`
See
the corpus file · open in the playground

A unique symbol would become any symbol

syntax.unique-symbol · decided in initializer-inference.ts isSymbolCall

TypeScript infers a unique symbol for a const holding Symbol(). The run only sees a symbol, and writing symbol would widen it.

Symbol("key") in a const

Variable key Not written: kept-existing

Before
const key = Symbol("key");
After ts-capture apply
const key = Symbol("key");

TypeScript types Symbol("key") in a const as a unique symbol, which can key a type. The run only sees a symbol. Writing symbol would widen it.

Observed
symbol · a `unique symbol` would be widened
See
the corpus file · open in the playground

An array's callback, typed by the array

syntax.array-callback-shape · decided in annotation-eligibility.ts suppressArrayCallbackStructural

A callback passed to an array method is typed by the array's element type. Writing the element's shape again adds a long type and nothing else. A primitive is still written.

Objects mapped by a callback

Parameter point Not written: redundant

Before
const xs = [{ x: 1 }, { x: 2 }].map((point) => point.x);
After ts-capture apply
const xs = [{ x: 1 }, { x: 2 }].map((point) => point.x);

The callback's parameter is typed by the array it is called on. Writing the element's shape again at the callback adds a long type and nothing else.

Observed
{ x: number }
See
the corpus file · open in the playground

Without a project, read from the initializer

syntax.redundant-initializer · decided in initializer-inference.ts inferTypeFromInitializer

With no tsconfig there is no type checker. Whether TypeScript would infer the same type is then read from the initializer's syntax. With a project the checker answers first, as gate.redundant.

No example on this page: the page runs every example the way the playground does, and this rule needs a run it cannot make.

From what the run saw

No call passed it

info.never-passed · decided in cst-replacements.ts buildCstReplacements

Every call left the parameter out, so all the run saw was undefined. Written as : undefined, it would make every call that does pass it an error.

An options parameter left out of every call

Parameter options Not written: no-information

Before
function connect(options) {
  return { retries: 3, ...options };
}

connect();
After ts-capture apply
function connect(options): { retries: number } {
  return { retries: 3, ...options };
}

connect();

No call passed this parameter, so all the run saw was undefined. That says nothing about what the parameter is for, and written as options: undefined it would make every call that does pass options an error.

Observed
undefined · every call left it out
See
the corpus file · open in the playground

Nothing left once the empty shapes go

info.nothing-left · decided in compute-annotation.ts computeAnnotation

A shape with an empty array as a field says nothing about what the array holds. Such shapes are dropped, and here nothing else was seen.

Only a shape with an empty array was seen

Parameter post Not written: no-information

Before
function countTags(post) {
  return post.tags.length;
}

countTags({ tags: [] });
After ts-capture apply
function countTags(post): number {
  return post.tags.length;
}

countTags({ tags: [] });

The one call passed { tags: [] }, and an empty array says nothing about what goes in it. Written as unknown[], the field would make every reader of it handle unknown, so the shape is dropped, and nothing is left to write.

Observed
{ tags: unknown[] }
See
the corpus file · open in the playground

Nothing worth writing

info.describes-nothing · decided in compute-annotation.ts carriesNoInformation

What the run saw says nothing about the value: an empty object, unknown, or a collection the run only saw empty.

A callback that returned an empty object

Callback job Not written: no-information

Before
function schedule(job) {
  return job();
}

schedule(() => JSON.parse("{}"));
After ts-capture apply
function schedule(job) {
  return job();
}

schedule(() => JSON.parse("{}"));

The function came back with an empty object, so all the run knows is () => {}, which says nothing about what the callback is for.

Observed
() => {}
See
the corpus file · open in the playground

A field that held an empty object

Class field blank Not written: no-information

Before
class Note {
  blank = JSON.parse("{}");
}

new Note();
After ts-capture apply
class Note {
  blank = JSON.parse("{}");
}

new Note();

All the run saw was an empty object, which says nothing about what the field is for.

Observed
{}
See
the corpus file · open in the playground

A function that returned an empty object

Return type Not written: no-information

Before
function parse(text) {
  return JSON.parse(text);
}

parse("{}");
After ts-capture apply
function parse(text: string) {
  return JSON.parse(text);
}

parse("{}");

All that came back was an empty object, which says nothing about what the function returns.

Observed
{}
See
the corpus file · open in the playground

Too long to be worth writing

info.too-long · decided in compute-annotation.ts computeAnnotation

The type is longer than maxAnnotationChars. An annotation that long is harder to read than none.

400 shapes passed to one parameter

Parameter record Not written: no-information

Before
function store(record) {
  return Object.keys(record).length;
}

for (let i = 0; i < 400; i++) store({ [`field${i}`]: i });
After ts-capture apply
function store(record) {
  return Object.keys(record).length;
}

for (let i = 0; i < 400; i++) store({ [`field${i}`]: i });

The calls passed 400 different shapes. Their union is longer than maxAnnotationChars (4096 characters by default), and an annotation that long is harder to read than no annotation.

Observed
{ field0: number } | { field1: number } | { field2: number } | { field3: number } | { field4: number } | { field5: number } | { field6: number } | { field7: number } | { field8: number } | { field9: number } | { field10: number } | { field11: number } | { field12: number } | { field13: number } | { field14: number } | { field15: number } | { field16: number } | { field17: number } | { field18: number } | { field19: number } | { field20: number } | { field21: number } | { field22: number } | { field23: number } | { field24: number } | { field25: number } | { field26: number } | { field27: number } | { field28: number } | { field29: number } | { field30: number } | { field31: number } | { field32: number } | { field33: number } | { field34: number } | { field35: number } | { field36: number } | { field37: number } | { field38: number } | { field39: number } | { field40: number } | { field41: number } | { field42: number } | { field43: number } | { field44: number } | { field45: number } | { field46: number } | { field47: number } | { field48: number } | { field49: number } | { field50: number } | { field51: number } | { field52: number } | { field53: number } | { field54: number } | { field55: number } | { field56: number } | { field57: number } | { field58: number } | { field59: number } | { field60: number } | { field61: number } | { field62: number } | { field63: number } | { field64: number } | { field65: number } | { field66: number } | { field67: number } | { field68: number } | { field69: number } | { field70: number } | { field71: number } | { field72: number } | { field73: number } | { field74: number } | { field75: number } | { field76: number } | { field77: number } | { field78: number } | { field79: number } | { field80: number } | { field81: number } | { field82: number } | { field83: number } | { field84: number } | { field85: number } | { field86: number } | { field87: number } | { field88: number } | { field89: number } | { field90: number } | { field91: number } | { field92: number } | { field93: number } | { field94: number } | { field95: number } | { field96: number } | { field97: number } | { field98: number } | { field99: number } | { field100: number } | { field101: number } | { field102: number } | { field103: number } | { field104: number } | { field105: number } | { field106: number } | { field107: number } | { field108: number } | { field109: number } | { field110: number } | { field111: number } | { field112: number } | { field113: number } | { field114: number } | { field115: number } | { field116: number } | { field117: number } | { field118: number } | { field119: number } | { field120: number } | { field121: number } | { field122: number } | { field123: number } | { field124: number } | { field125: number } | { field126: number } | { field127: number } | { field128: number } | { field129: number } | { field130: number } | { field131: number } | { field132: number } | { field133: number } | { field134: number } | { field135: number } | { field136: number } | { field137: number } | { field138: number } | { field139: number } | { field140: number } | { field141: number } | { field142: number } | { field143: number } | { field144: number } | { field145: number } | { field146: number } | { field147: number } | { field148: number } | { field149: number } | { field150: number } | { field151: number } | { field152: number } | { field153: number } | { field154: number } | { field155: number } | { field156: number } | { field157: number } | { field158: number } | { field159: number } | { field160: number } | { field161: number } | { field162: number } | { field163: number } | { field164: number } | { field165: number } | { field166: number } | { field167: number } | { field168: number } | { field169: number } | { field170: number } | { field171: number } | { field172: number } | { field173: number } | { field174: number } | { field175: number } | { field176: number } | { field177: number } | { field178: number } | { field179: number } | { field180: number } | { field181: number } | { field182: number } | { field183: number } | { field184: number } | { field185: number } | { field186: number } | { field187: number } | { field188: number } | { field189: number } | { field190: number } | { field191: number } | { field192: number } | { field193: number } | { field194: number } | { field195: number } | { field196: number } | { field197: number } | { field198: number } | { field199: number } | { field200: number } | { field201: number } | { field202: number } | { field203: number } | { field204: number } | { field205: number } | { field206: number } | { field207: number } | { field208: number } | { field209: number } | { field210: number } | { field211: number } | { field212: number } | { field213: number } | { field214: number } | { field215: number } | { field216: number } | { field217: number } | { field218: number } | { field219: number } | { field220: number } | { field221: number } | { field222: number } | { field223: number } | { field224: number } | { field225: number } | { field226: number } | { field227: number } | { field228: number } | { field229: number } | { field230: number } | { field231: number } | { field232: number } | { field233: number } | { field234: number } | { field235: number } | { field236: number } | { field237: number } | { field238: number } | { field239: number } | { field240: number } | { field241: number } | { field242: number } | { field243: number } | { field244: number } | { field245: number } | { field246: number } | { field247: number } | { field248: number } | { field249: number } | { field250: number } | { field251: number } | { field252: number } | { field253: number } | { field254: number } | { field255: number } | { field256: number } | { field257: number } | { field258: number } | { field259: number } | { field260: number } | { field261: number } | { field262: number } | { field263: number } | { field264: number } | { field265: number } | { field266: number } | { field267: number } | { field268: number } | { field269: number } | { field270: number } | { field271: number } | { field272: number } | { field273: number } | { field274: number } | { field275: number } | { field276: number } | { field277: number } | { field278: number } | { field279: number } | { field280: number } | { field281: number } | { field282: number } | { field283: number } | { field284: number } | { field285: number } | { field286: number } | { field287: number } | { field288: number } | { field289: number } | { field290: number } | { field291: number } | { field292: number } | { field293: number } | { field294: number } | { field295: number } | { field296: number } | { field297: number } | { field298: number } | { field299: number } | { field300: number } | { field301: number } | { field302: number } | { field303: number } | { field304: number } | { field305: number } | { field306: number } | { field307: number } | { field308: number } | { field309: number } | { field310: number } | { field311: number } | { field312: number } | { field313: number } | { field314: number } | { field315: number } | { field316: number } | { field317: number } | { field318: number } | { field319: number } | { field320: number } | { field321: number } | { field322: number } | { field323: number } | { field324: number } | { field325: number } | { field326: number } | { field327: number } | { field328: number } | { field329: number } | { field330: number } | { field331: number } | { field332: number } | { field333: number } | { field334: number } | { field335: number } | { field336: number } | { field337: number } | { field338: number } | { field339: number } | { field340: number } | { field341: number } | { field342: number } | { field343: number } | { field344: number } | { field345: number } | { field346: number } | { field347: number } | { field348: number } | { field349: number } | { field350: number } | { field351: number } | { field352: number } | { field353: number } | { field354: number } | { field355: number } | { field356: number } | { field357: number } | { field358: number } | { field359: number } | { field360: number } | { field361: number } | { field362: number } | { field363: number } | { field364: number } | { field365: number } | { field366: number } | { field367: number } | { field368: number } | { field369: number } | { field370: number } | { field371: number } | { field372: number } | { field373: number } | { field374: number } | { field375: number } | { field376: number } | { field377: number } | { field378: number } | { field379: number } | { field380: number } | { field381: number } | { field382: number } | { field383: number } | { field384: number } | { field385: number } | { field386: number } | { field387: number } | { field388: number } | { field389: number } | { field390: number } | { field391: number } | { field392: number } | { field393: number } | { field394: number } | { field395: number } | { field396: number } | { field397: number } | { field398: number } | { field399: number }
See
the corpus file · open in the playground

A type TypeScript cannot parse

info.unparseable · decided in parseable.ts isParseableTypeString

The observed type would not parse as TypeScript, as when a class was renamed at runtime to something no identifier can be.

A name no type can have

Parameter item Not written: no-information

Before
function label(item) {
  return String(item);
}

const Renamed = Object.defineProperty(class {}, "name", { value: "9lives" });
label(new Renamed());
After ts-capture apply
function label(item) {
  return String(item);
}

const Renamed = Object.defineProperty(class {}, "name", { value: "9lives" });
label(new Renamed());

The class was renamed at runtime to 9lives, which no identifier can be. The type would not parse, so nothing is written.

Observed
9lives · not a type TypeScript can parse
See
the corpus file · open in the playground

A name the code cannot see

info.out-of-scope · decided in scope-reachability.ts allTypeRefsInScope

The type names something declared where this position cannot reach it, such as a class inside another function. Written, it would not compile.

A class declared at the call

Callback factory Not written: out-of-scope

Before
function later(factory) {
  return new factory();
}

later(class Job {});
After ts-capture apply
function later(factory) {
  return new factory();
}

later(class Job {});

The parameter received a class declared at the call. Its name means nothing where the parameter is.

Observed
typeof Job · Job is not in scope
See
the corpus file · open in the playground

A field holding a class local to a callback

Class field made Not written: out-of-scope

Before
function build(factory) {
  return factory();
}

class Holder {
  made = build(() => {
    class Box {
      size = 1;
    }
    return new Box();
  });
}

new Holder();
After ts-capture apply
function build(factory) {
  return factory();
}

class Holder {
  made = build(() => {
    class Box {
      size = 1;
    }
    return new Box();
  });
}

new Holder();

The field held an instance of a class declared inside a callback. Its name means nothing in Holder.

Observed
Box · Box is not in scope
See
the corpus file · open in the playground

A class local to another function

Parameter shape Not written: out-of-scope

Before
function makeShape() {
  class Circle {
    radius = 1;
  }
  return new Circle();
}

function area(shape) {
  return Math.PI * shape.radius ** 2;
}

area(makeShape());
After ts-capture apply
function makeShape() {
  class Circle {
    radius = 1;
  }
  return new Circle();
}

function area(shape) {
  return Math.PI * shape.radius ** 2;
}

area(makeShape());

The value was an instance of a class declared inside another function. Its name means nothing where the parameter is, so writing it would not compile.

Observed
Circle · Circle is not in scope
See
the corpus file · open in the playground

Returning a class local to a callback

Return type Not written: out-of-scope

Before
function build(factory) {
  return factory();
}

build(() => {
  class Box {
    size = 1;
  }
  return new Box();
});
After ts-capture apply
function build(factory) {
  return factory();
}

build(() => {
  class Box {
    size = 1;
  }
  return new Box();
});

What came back was an instance of a class declared inside the callback. Its name means nothing at build, so writing it would not compile.

Observed
Box · Box is not in scope
See
the corpus file · open in the playground

A variable holding a class local to a callback

Variable made Not written: out-of-scope

Before
function build(factory) {
  const made = factory();
  return made;
}

build(() => {
  class Box {
    size = 1;
  }
  return new Box();
});
After ts-capture apply
function build(factory) {
  const made = factory();
  return made;
}

build(() => {
  class Box {
    size = 1;
  }
  return new Box();
});

The value was an instance of a class declared inside the callback. Its name means nothing where made is declared.

Observed
Box · Box is not in scope
See
the corpus file · open in the playground

From what TypeScript infers

A method that returns this

gate.polymorphic-this · decided in redundant-annotation.ts carriesPolymorphicThis

TypeScript types the position as this, which in a subclass is the subclass. Writing the class the run saw would cost every subclass its own type.

A builder that chains with return this

Return type Not written: kept-existing

Before
class Builder {
  parts: string[] = [];
  add(part: string) {
    this.parts.push(part);
    return this;
  }
}

new Builder().add("a");
After ts-capture apply
class Builder {
  parts: string[] = [];
  add(part: string) {
    this.parts.push(part);
    return this;
  }
}

new Builder().add("a");

TypeScript types the return as this, which in a subclass is the subclass. The run saw a Builder. Writing Builder would make every subclass's chained calls return the base class.

Observed
Builder · TypeScript has this
See
the corpus file · open in the playground

A signature would be replaced by Function

gate.call-signature · decided in redundant-annotation.ts writesOverCallSignature

The run can only say a value was callable. Where TypeScript already has its signature, Function would throw it away.

The resolve a promise hands its executor

Callback resolve Not written: kept-existing

Before
const ready = new Promise((resolve) => resolve(1));
After ts-capture apply
const ready = new Promise((resolve) => resolve(1));

resolve is a function the engine made, so the run can only say it was callable. TypeScript already has its signature, and Function would throw that away.

Observed
Function · TypeScript has (value: unknown) => void
See
the corpus file · open in the playground

A name would be replaced by what it is made of

gate.erases-named-type · decided in redundant-annotation.ts erasesNamedType

TypeScript has a named type here, an interface, alias or type parameter. Writing the shape the run saw would throw the name away.

An interface the run saw one field more on

Parameter user Not written: kept-existing

Before
interface User {
  name: string;
}

function each(visit: (user: User) => void) {
  visit({ name: "Ada", age: 36 } as User);
}

each((user) => user.name);
After ts-capture apply
interface User {
  name: string;
}

function each(visit: (user: User) => void) {
  visit({ name: "Ada", age: 36 } as User);
}

each((user) => user.name);

TypeScript types user as the interface User from where the callback is passed. The run saw a shape with one more field. Writing that shape would throw the name User away, and with it everything the interface says.

Observed
{ age: number, name: string } · TypeScript has User
See
the corpus file · open in the playground

An enum would become its values

gate.erases-enum · decided in redundant-annotation.ts erasesEnum

TypeScript types the position as an enum. The run only sees the string or number underneath, and writing that would throw the enum away.

A field holding an enum member

Class field mode Not written: kept-existing

Before
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

class Runner {
  mode = [Mode.Fast, Mode.Slow][0];
}

new Runner();
After ts-capture apply
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

class Runner {
  mode = [Mode.Fast, Mode.Slow][0];
}

new Runner();

TypeScript infers the enum Mode. The run only sees the string underneath, and writing string would throw the enum away.

Observed
string · TypeScript has Mode
See
the corpus file · open in the playground

A callback parameter typed as an enum

Parameter mode Not written: kept-existing

Before
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

const modes = [Mode.Fast, Mode.Slow];
modes.forEach((mode) => mode.length);
After ts-capture apply
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

const modes = [Mode.Fast, Mode.Slow];
modes.forEach((mode) => mode.length);

TypeScript types this parameter as the enum Mode. The run only sees the string underneath. Writing string would throw the enum away, and with it every comparison against Mode.Fast.

Observed
string · TypeScript has Mode
See
the corpus file · open in the playground

A function returning an enum member

Return type Not written: kept-existing

Before
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

function pick(quick) {
  return quick ? Mode.Fast : Mode.Slow;
}

pick(true);
After ts-capture apply
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

function pick(quick: boolean) {
  return quick ? Mode.Fast : Mode.Slow;
}

pick(true);

TypeScript infers the enum Mode. The run only sees the string underneath, and writing string would throw the enum away.

Observed
string · TypeScript has Mode
See
the corpus file · open in the playground

A variable holding an enum member

Variable mode Not written: kept-existing

Before
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

const mode = [Mode.Fast, Mode.Slow][0];
After ts-capture apply
enum Mode {
  Fast = "fast",
  Slow = "slow",
}

const mode = [Mode.Fast, Mode.Slow][0];

TypeScript infers the enum Mode. The run only sees the string underneath, and writing string would throw the enum away.

Observed
string · TypeScript has Mode
See
the corpus file · open in the playground

A value that was one arm of what TypeScript infers

gate.discards-union-arm · decided in redundant-annotation.ts discardsUnionArm

TypeScript infers a union, and the run saw only some of its arms. Writing those would forbid the rest, so the type is left to TypeScript, which already holds every value the run saw.

Only null back from a call that can return an array

Variable fromNull Not written: left-to-typescript

Before
function valuesOrNull(...values) {
  if (!values[0]) {
    return null;
  }
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);
const fromNull = valuesOrNull(null);
After ts-capture apply
function valuesOrNull(...values: (number | string | null)[]): (number | string | null)[] | null {
  if (!values[0]) {
    return null;
  }
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);
const fromNull = valuesOrNull(null);

The tests only ever got null back here, but TypeScript knows the call can also return the array. Writing null would forbid the array, so the type is left to TypeScript, which already holds both.

Observed
null · TypeScript has (string | number | null)[] | null
See
the corpus file · open in the playground

The run disagrees with TypeScript

gate.contradiction · decided in redundant-annotation.ts observedBeyondInferred

The run saw null or undefined where TypeScript's type says it cannot be. Apply writes neither type, and reports the disagreement.

A typed callback once passed undefined

Callback next Not written: contradiction

Before
const step: (go: () => void) => void = (next) => next?.();

step(() => {});
step(undefined as unknown as () => void);
After ts-capture apply
// @ts-capture[conflict]: `next` observed `undefined`, TypeScript infers `() => void`
const step: (go: () => void) => void = (next) => next?.();

step(() => {});
step(undefined as unknown as () => void);

TypeScript types next from the signature the callback is assigned to: a function, never missing. The run passed undefined once. Apply reports the disagreement instead of widening the parameter.

Observed
() => void | undefined · the type here is () => void
See
the corpus file · open in the playground

A field read one past the end of an array

Class field word Not written: contradiction

Before
const words = ["a"];
let next = 0;

class Slot {
  word = words[next++];
}

new Slot();
new Slot();
After ts-capture apply
const words = ["a"];
let next = 0;

class Slot {
  // @ts-capture[conflict]: `word` observed `undefined`, TypeScript infers `string`
  word = words[next++];
}

new Slot();
new Slot();

TypeScript types an element of a string[] as string. The second instance read one past the end, so the run saw undefined too.

Observed
string | undefined · the type here is string
See
the corpus file · open in the playground

A string callback parameter given undefined

Parameter word Not written: contradiction

Before
function each(words: string[], visit: (value: string) => void) {
  for (const word of words) visit(word);
}

const words = ["a", "b"];
words.push(undefined as unknown as string);
each(words, (word) => word);
After ts-capture apply
function each(words: string[], visit: (value: string) => void) {
  for (const word of words) visit(word);
}

const words = ["a", "b"];
words.push(undefined as unknown as string);
each(words, (word /* @ts-capture[conflict]: observed `undefined`, TypeScript infers `string` */) /* @ts-capture[conflict]: observed `undefined`, TypeScript infers `string` */ => word);

TypeScript already types this callback parameter as string, but the run passed undefined too. Apply does not write a type the checker contradicts; it reports the disagreement instead.

Observed
string | undefined · the type here is string
See
the corpus file · open in the playground

A field the test's object did not have

Return type Not written: contradiction

Before
function nameOf(user: { name: string }) {
  return user.name;
}

nameOf(JSON.parse("{}"));
After ts-capture apply
// @ts-capture[conflict]: `nameOf return` observed `void`, TypeScript infers `string`
function nameOf(user: { name: string }) {
  return user.name;
}

nameOf(JSON.parse("{}"));

TypeScript reads the return as string from the parameter's type. The run got undefined, because a test passed an object without the field. Apply reports that instead of writing either.

Observed
undefined · the type here is string
See
the corpus file · open in the playground

A variable read one past the end of an array

Variable word Not written: contradiction

Before
const words = ["a"];
for (const i of [0, 1]) {
  const word = words[i];
}
After ts-capture apply
const words = ["a"];
for (const i of [0, 1]) {
  // @ts-capture[conflict]: `word` observed `undefined`, TypeScript infers `string`
  const word = words[i];
}

TypeScript types an element of a string[] as string. The loop read one past the end, so the run saw undefined too. Apply reports the disagreement instead of writing either.

Observed
string | undefined · the type here is string
See
the corpus file · open in the playground
In real code: A disagreement, reported and not written

Left alone honojs/hono src/middleware/cache/index.ts:301

Before
      typeof options.cacheName === 'function' ? await options.cacheName(c) : options.cacheName
    const cache = await caches.open(cacheName)
    const response = await cache.match(key)
    if (response) {
After ts-capture apply
      typeof options.cacheName === 'function' ? await options.cacheName(c) : options.cacheName
    const cache = await caches.open(cacheName)
    // @ts-capture[conflict]: `response` observed `null`, TypeScript infers `Response | undefined`
    const response = await cache.match(key)
    if (response) {

In the tests cache.match gave null, where TypeScript infers Response or undefined. ts-capture does not write a type the checker contradicts. It leaves a note saying what it saw.

TypeScript already knows

gate.redundant · decided in redundant-annotation.ts isRedundantAnnotation

TypeScript infers the same type here already. Writing it adds nothing, unless skipRedundantAnnotations is off.

A callback whose type is a function alias

Callback cond Not written: redundant

Before
interface Context {
  path: string;
}

type Condition = (c: Context) => boolean;

function matchesAny(...conditions: Condition[]) {
  return (c: Context) => conditions.some((cond) => cond(c));
}

matchesAny((c: Context) => c.path === "/")({ path: "/" });
After ts-capture apply
interface Context {
  path: string;
}

type Condition = (c: Context) => boolean;

function matchesAny(...conditions: Condition[]) {
  return (c: Context) => conditions.some((cond) => cond(c));
}

matchesAny((c: Context) => c.path === "/")({ path: "/" });

cond gets its type from conditions, which TypeScript prints by the alias name Condition. The run saw the alias's body, (c: Context) => boolean: the same type, so there is nothing to write.

Observed
(c: Context) => boolean
See
the corpus file · open in the playground

A callback whose type names a generic with defaults

Callback cond Not written: redundant

Before
interface Context<Env = any, Path extends string = any> {
  env: Env;
  path: Path;
}

function matchesAny(...conditions: ((c: Context) => boolean)[]) {
  return (c: Context) => conditions.some((cond) => cond(c));
}

matchesAny((c: Context) => c.path === "/")({ env: {}, path: "/" });
After ts-capture apply
interface Context<Env = any, Path extends string = any> {
  env: Env;
  path: Path;
}

function matchesAny(...conditions: ((c: Context) => boolean)[]) {
  return (c: Context) => conditions.some((cond) => cond(c));
}

matchesAny((c: Context) => c.path === "/")({ env: {}, path: "/" });

cond gets its type from conditions, and the run saw the same. TypeScript spells it (c: Context<any, any>) => boolean away from the code and (c: Context) => boolean where cond is, so it is the type TypeScript has.

Observed
(c: Context) => boolean
See
the corpus file · open in the playground

A callback over an array of functions

Callback task Not written: redundant

Before
const tasks = [() => 1];
tasks.forEach((task) => task());
After ts-capture apply
const tasks = [() => 1];
tasks.forEach((task) => task());

The array's type says what each element is, so TypeScript types the callback's parameter already.

Observed
() => number
See
the corpus file · open in the playground

A field assigned in the constructor

Class field count Not written: redundant

Before
class Counter {
  count;
  constructor(start) {
    this.count = start;
  }
}

new Counter(5);
After ts-capture apply
class Counter {
  count;
  constructor(start: number) {
    this.count = start;
  }
}

new Counter(5);

Once the constructor's parameter has a type, TypeScript infers the field from the assignment in the constructor. Writing it again adds nothing.

Observed
number
See
the corpus file · open in the playground

A map callback over strings

Parameter word Not written: redundant

Before
const lengths = ["a", "bb"].map((word) => word.length);
After ts-capture apply
const lengths = ["a", "bb"].map((word) => word.length);

A callback's parameter is typed by where the callback is passed. Writing the same type again adds nothing.

Observed
string
See
the corpus file · open in the playground

From type-checking the file

A type the code would not compile with

verify.type-error · decided in apply-types-verify.ts filterAcceptedReplacements

Apply type-checks every annotation with the others written. One that would add an error is dropped, and the report gives the error.

A callback parameter set to null later

Callback job Not written: type-error

Before
function retry(job) {
  const result = job();
  job = null;
  return result;
}

retry(() => 1);
After ts-capture apply
function retry(job): number {
  const result: number = job();
  job = null;
  return result;
}

retry(() => 1);

The run saw a function returning number. The body later sets job to null, which that type would make an error.

Observed
() => number
See
the corpus file · open in the playground

A field a method sets to null

Class field data Not written: type-error

Before
class Store {
  data = JSON.parse('{"a": 1}');
  reset() {
    this.data = null;
  }
}

new Store();
After ts-capture apply
class Store {
  data = JSON.parse('{"a": 1}');
  reset() {
    this.data = null;
  }
}

new Store();

The run saw { a: number }, but reset sets the field to null. Written, the type would make that line an error, so apply drops it.

Observed
{ a: number }
See
the corpus file · open in the playground

A parameter the function adds a field to

Parameter settings Not written: type-error

Before
function enable(settings) {
  settings.enabled = true;
  return settings;
}

enable({ name: "dark mode" });
After ts-capture apply
function enable(settings): { enabled: boolean; name: string } {
  settings.enabled = true;
  return settings;
}

enable({ name: "dark mode" });

The run saw { name: string }, but the function adds a field. Written, that type would make the next line an error, so apply type-checks every annotation first and drops this one.

Observed
{ name: string }
See
the corpus file · open in the playground
In real code: A position it observed and did not write

Left alone mini-app src/inventory.ts:9

Before
}

export function lowStock(stock, threshold) {
  return Object.keys(stock).filter((sku) => stock[sku] < threshold);
}
After ts-capture apply
}

export function lowStock(stock, threshold: number) {
  return Object.keys(stock).filter((sku) => stock[sku] < threshold);
}

threshold is written; stock is not. The observed shape would have added a type error on the next line, so apply left the parameter as it was and said so in its report.

Not written
stock · would add a type error
Observed
{ "sku-chair": number, "sku-lamp": number }
Why
TS7053 at src/inventory.ts:10: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ "sku-chair": number; "sku-lamp": number; }'. No index signature with a parameter of type 'stri…

A caller the tests never ran

Return type Not written: type-error

Before
function parse(text) {
  return JSON.parse(text);
}

parse("1");

function label(): string {
  return parse('"a"');
}
After ts-capture apply
function parse(text: string) {
  return JSON.parse(text);
}

parse("1");

function label(): string {
  return parse('"a"');
}

Every call returned a number, so that is what the run saw. But label hands the result on as a string, and written, the type would make that line an error.

Observed
number
See
the corpus file · open in the playground

A variable set to null later

Variable current Not written: type-error

Before
let current = JSON.parse('{"n": 1}');
current = null;
After ts-capture apply
let current = JSON.parse('{"n": 1}');
current = null;

The run saw current hold { n: number }. Written, that type would make the assignment of null further down an error, so apply drops it.

Observed
{ n: number }
See
the corpus file · open in the playground

TypeScript has a wider type once the rest is written

verify.wider-after-run · decided in apply-types-verify.ts widerTypeWithOthers

Judged with the run's other annotations written, TypeScript infers a type that holds everything the run saw, and is wider. A variable is left to TypeScript. A return type is written as TypeScript has it, unless TypeScript inferred it before the run too.

Narrower than the call it came from

Variable fromNull Not written: left-to-typescript

Before
function valuesOrNull(...values) {
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);
const fromNull = valuesOrNull(null);
After ts-capture apply
function valuesOrNull(...values: (number | string | null)[]): (number | string | null)[] {
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);
const fromNull = valuesOrNull(null);

This call returned null[]. Once valuesOrNull has its return type, TypeScript infers the wider array for the variable, and that holds what the run saw. Writing null[] would only narrow it.

Observed
null[] · TypeScript infers a wider type once the other annotations are written
See
the corpus file · open in the playground

The same, with restatements asked for

Return type Written

Before
function findUser(id: number) {
  if (id < 0) return null;
  return { id };
}

findUser(1);
After ts-capture apply
function findUser(id: number): { id: number } | null {
  if (id < 0) return null;
  return { id };
}

findUser(1);

With skipRedundantAnnotations off, apply writes types TypeScript already infers. That includes this return, as TypeScript has it.

Observed
{ id: number } | null
See
the corpus file · open in the playground

A branch the tests never took, already known

Return type Not written: left-to-typescript

Before
function findUser(id: number) {
  if (id < 0) return null;
  return { id };
}

findUser(1);
After ts-capture apply
function findUser(id: number) {
  if (id < 0) return null;
  return { id };
}

findUser(1);

The tests never took return null, so the run saw only the object. TypeScript inferred { id: number } | null before the run already, so writing it would only repeat it.

Observed
{ id: number } · TypeScript infers a wider type once the other annotations are written
See
the corpus file · open in the playground

A returned function with a generic's defaults, already known

Return type Not written: left-to-typescript

Before
interface Request<Body = any> {
  body: Body;
}

function handlerFor(prefix: string) {
  return async (request: Request, reply: (text: string) => number) => {
    if (!request.body) return reply(prefix);
    reply(prefix + request.body);
  };
}

handlerFor("Hi ")({ body: "" }, (text) => text.length);
After ts-capture apply
interface Request<Body = any> {
  body: Body;
}

function handlerFor(prefix: string) {
  return async (request: Request, reply: (text: string) => number) => {
    if (!request.body) return reply(prefix);
    reply(prefix + request.body);
  };
}

handlerFor("Hi ")({ body: "" }, (text) => text.length);

The run only saw the branch that returns a number, so Promise<number> does not hold. The returned function's parameters are typed in the code, so TypeScript knew its type before the run. Read from outside the function it spells Request<any>, at the return Request, but it is one type, and it is left to TypeScript.

Observed
(request: Request, reply: (text: string) => number) => Promise<number> · TypeScript infers a wider type once the other annotations are written
See
the corpus file · open in the playground

A branch the tests never took

Return type Written

Before
function valuesOrNull(...values) {
  if (!values[0]) {
    return null;
  }
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);
const fromNull = valuesOrNull(null);
After ts-capture apply
function valuesOrNull(...values: (number | string | null)[]): (number | string | null)[] | null {
  if (!values[0]) {
    return null;
  }
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);
const fromNull = valuesOrNull(null);

The tests never took the return null branch, so what the run saw would not compile with it. Once values is written, TypeScript infers a type that holds every value the run saw, and that is what is written.

Observed
(number | string | null)[] | null
See
the corpus file · open in the playground

Written

Written

written · decided in cst-replacements.ts buildCstReplacements

The run saw a type, and nothing TypeScript already knows says more. It is written.

Different functions at each call

Callback produce Written

Before
function resultOf(produce) {
  return produce();
}
resultOf(() => 1);
resultOf(() => "one");
After ts-capture apply
function resultOf(produce: (() => number) | (() => string)): number | string {
  return produce();
}
resultOf(() => 1);
resultOf(() => "one");

One call passed a function returning a number, the other one returning a string. The parameter accepts either.

Observed
(() => number) | (() => string)
See
the corpus file · open in the playground

A destructuring, named by its pattern

Variable { size } Written

Before
const { size } = JSON.parse('{ "size": 3 }');
console.log(size);
After ts-capture apply
const { size }: { size: number } = JSON.parse('{ "size": 3 }');
console.log(size);

The parsed value has no type TypeScript knows, so the pattern takes the shape the run saw. The report names the site by its pattern, { size }.

Observed
{ size: number }
See
the corpus file · open in the playground

What the field turned out to hold

Class field items Written

Before
class Store {
  items = JSON.parse("[1, 2]");
}

new Store();
After ts-capture apply
class Store {
  items: number[] = JSON.parse("[1, 2]");
}

new Store();

JSON.parse returns any, so TypeScript knows nothing about items. The run saw the array it held.

Observed
number[]
See
the corpus file · open in the playground

A list whose items differ, inside an object

Parameter order Written

Before
function ship(order) {
  return order.lines.length;
}

ship({
  id: 7,
  lines: [
    { sku: "A1", qty: 2 },
    { sku: "B2", qty: 1, note: "gift" },
  ],
});
After ts-capture apply
function ship(order: { id: number; lines: { note?: string; qty: number; sku: string }[] }): number {
  return order.lines.length;
}

ship({
  id: 7,
  lines: [
    { sku: "A1", qty: 2 },
    { sku: "B2", qty: 1, note: "gift" },
  ],
});

The order's lines were not all alike: one had a note, one did not. They are written as one kind of line where the note may be missing, the same as for a list passed on its own.

Observed
{ id: number; lines: { note?: string; qty: number; sku: string }[] }
See
the corpus file · open in the playground

Lists that differ between calls

Parameter lines Written

Before
function count(lines) {
  return lines.length;
}

count([{ sku: "A1", qty: 2 }]);
count([{ sku: "B2", qty: 1, note: "gift" }]);
After ts-capture apply
function count(lines: { note?: string; qty: number; sku: string }[]): number {
  return lines.length;
}

count([{ sku: "A1", qty: 2 }]);
count([{ sku: "B2", qty: 1, note: "gift" }]);

Each call passed a list of lines, and only the second had a note. The parameter is written as a list of one kind of line where the note may be missing, which both calls fit.

Observed
{ note?: string; qty: number; sku: string }[]
See
the corpus file · open in the playground

Lists that differ, where one call had none

Parameter order Written

Before
function ship(order) {
  return order.lines?.length ?? 0;
}

ship({ lines: [{ sku: "A1", qty: 2 }] });
ship({ lines: null });
ship({ lines: [{ sku: "B2", qty: 1, note: "gift" }] });
After ts-capture apply
function ship(order: { lines: { note?: string; qty: number; sku: string }[] | null }): number {
  return order.lines?.length ?? 0;
}

ship({ lines: [{ sku: "A1", qty: 2 }] });
ship({ lines: null });
ship({ lines: [{ sku: "B2", qty: 1, note: "gift" }] });

One order had plain lines, one had none, and one had a line with a note. The lines are written as one kind of line where the note may be missing, and the missing list stays a null beside them.

Observed
{ lines: { note?: string; qty: number; sku: string }[] | null }
See
the corpus file · open in the playground

What every call passed

Parameter user Written

Before
function greet(user) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });
greet({ name: "Bo" });
After ts-capture apply
function greet(user: { name: string }) {
  return "Hello " + user.name;
}

greet({ name: "Ada" });
greet({ name: "Bo" });

The run saw one shape at this parameter, and nothing in TypeScript already says more.

Observed
{ name: string }
See
the corpus file · open in the playground
In real code
Parameters and a return type, from the calls the tests made, A callback, typed from what it returned, An element of a collection TypeScript knows nothing about, A type that already has a name

What came back from a rest parameter

Return type Written

Before
function valuesOrNull(...values) {
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);
After ts-capture apply
function valuesOrNull(...values: (number | string)[]): (number | string)[] {
  return values;
}
valuesOrNull(1, 2);
valuesOrNull("1", 2);

Every call returned the arguments it was given, so the parameter and the return are the arrays the run saw.

Observed
(number | string)[]
See
the corpus file · open in the playground

A generic named the way the code names it

Return type Written

Before
interface Request<Body = any> {
  body: Body;
}

function handlerFor(prefix: string) {
  return (request: Request, greeting) => prefix + greeting + request.body;
}

handlerFor("Hi ")({ body: "Ada" }, "hello ");
After ts-capture apply
interface Request<Body = any> {
  body: Body;
}

function handlerFor(prefix: string): (request: Request, greeting: string) => string {
  return (request: Request, greeting: string) => prefix + greeting + request.body;
}

handlerFor("Hi ")({ body: "Ada" }, "hello ");

The returned function takes a Request, and every type parameter of Request has a default. TypeScript knows nothing of greeting, so the return type is written, and it says Request as the code does, not Request<unknown>.

Observed
(request: Request, greeting: string) => string
See
the corpus file · open in the playground

What came back, where TypeScript knows nothing

Return type Written

Before
function first(list) {
  return list[0];
}

first([{ id: 1, name: "Ada" }]);
After ts-capture apply
function first(list: { id: number; name: string }[]): { id: number; name: string } {
  return list[0];
}

first([{ id: 1, name: "Ada" }]);

The function hands back an element of a parameter TypeScript has no type for, so it infers any. The run saw what actually came back.

Observed
{ id: number; name: string }
See
the corpus file · open in the playground
In real code
Parameters and a return type, from the calls the tests made

this in a plain function

this Written

Before
function describe() {
  return this.name;
}

describe.call({ name: "Ada" });
After ts-capture apply
function describe(this: { name: string }): string {
  return this.name;
}

describe.call({ name: "Ada" });

A function that reads this without being a method has an implicit any this. With the instrumenter asked to look for it, the run sees what this was and apply writes it as the first parameter.

Observed
{ name: string }
See
the corpus file

What the value turned out to be

Variable data Written

Before
const data = JSON.parse('{"id": 1, "name": "Ada"}');

data.name;
After ts-capture apply
const data: { id: number; name: string } = JSON.parse('{"id": 1, "name": "Ada"}');

data.name;

JSON.parse returns any, so TypeScript knows nothing about data. The run saw the object it held.

Observed
{ id: number; name: string }
See
the corpus file · open in the playground