Developer troubleshooting

JSON-to-TypeScript output contains any[] or null: fix the sample before trusting the type

Diagnose empty-array and null output with three small JSON fixtures, then separate generator behavior from the API contract before copying declarations into a project.

By: ToolboxHub Editorial Team Sources checked: 5 min read 1100 words

If an empty JSON array becomes any[], the sample supplies no element values to inspect. If a property becomes null, that is the value the generator saw; neither result proves what a future API response is allowed to contain.

Start with a small fixture that reproduces the output

Reduce the response to the problematic properties before changing a declaration. For example, the illustrative input {"items":[],"next":null} should produce items: any[]; and next: null; inside the root declaration. There is no evidence here that items are strings or that next is a URL.

Open JSON to TypeScript Interface, set the root name to Page, select interface, and paste that exact input. The output should contain interface Page and both property lines. Keep a copy of the input alongside the output so a later change can be explained rather than attributed to a mysterious generator setting.

These fixtures are teaching examples. The behavior described here was checked against the current generator implementation; the examples do not represent requests to a real API or a successful application build. You can reproduce them in the tool without supplying credentials, customer data, or a production response.

Change one value to identify what inference actually uses

Replacing the empty array with representative elements changes the inferred element type. Paste {"items":["bolt","washer"],"next":null} with the same root name. Expect items: string[];, while next remains null. A nonempty array tells the tool something about its present elements, not about every allowed future element.

Now paste {"items":["bolt",7],"next":null}. The expected items property is items: (string | number)[];. This is an array whose elements can have either listed type, rather than an assertion that every first element is text and every second element is numeric.

As a reproducible verification, restore the first fixture after trying the other two. The items property should return to any[]. This shows that earlier samples are not accumulated into a lasting API model. Use Text Diff on saved output snippets if a nested response makes the changed property difficult to spot.

Do not insert invented production values merely to force attractive output. A fictional fixture is useful for learning the tool; a project declaration needs documented field rules or approved representative responses. If the array element contract is unknown, record that uncertainty and ask the API owner before treating a guessed type as an integration requirement.

Keep nullability and missing properties separate

A present property with a null value is different from a property omitted entirely. Compare {"next":null} with {}: the first has a required next: null; property, while the second supplies no next property for the generator to emit. The tool does not combine those samples into an optional property.

If the actual contract permits a string, null, or omission, a reviewer might write next?: string | null in the project. That is an illustrative contract decision, not output the generator learned from one response. The TypeScript handbook explains optional properties, unions, and how strictNullChecks affects handling of null and undefined. Check the project's setting before assuming a nullable type will be enforced as expected.

Changing Output mode from interface to type changes declaration syntax; it does not add missing API knowledge. Likewise, the root name labels the result but does not affect whether a sample contains an empty array or a missing key.

Recognize the failure symptoms before copying output

An apparently successful conversion can still produce an unsuitable project type. Treat the following symptoms as specific review tasks rather than reasons to replace every troublesome property with any.

  • Array members remain unchecked: inspect whether the source array was empty. Replace the generated any[] in the project only after establishing the element contract. TypeScript documents that any allows unchecked operations; it is not a promise of safety.
  • Several item declarations appear: arrays of objects are processed object by object, with separate names when needed. The generator does not merge variants into one carefully designed model with optional fields. Review the actual objects and decide the shared contract manually.
  • The output disappears and Invalid JSON appears: correct JSON syntax first. Remove trailing commas and use quoted property names; do not paste a JavaScript object literal or an entire HTTP response including headers. The JSON Formatter can help inspect a non-sensitive sample.
  • A status becomes string instead of a fixed list: the sample value is treated as a runtime string. Allowed business states must come from the API specification; a single value cannot establish all valid states.

For a deeply nested or very large response, begin with a small relevant branch. The implementation recursively inspects values, so browser memory and the JavaScript call stack impose practical limits. A shorter fixture also makes it easier to distinguish a naming artifact from a real difference in field structure.

Verify the contract in the project after generation

Before adopting the result, assemble approved examples for a populated array, an empty array, a nullable value, and an omitted property where the contract permits one. Check each against the reviewed declaration with the project's own TypeScript configuration. Also try an intentionally invalid value, such as a numeric next field when only text or null is permitted; it should be rejected by the intended type check.

This is a verification procedure to perform in your project, not a claim that those compiler checks ran here. The browser tool does not invoke your compiler, read your configuration, validate future HTTP responses, or edit project files. Copying its output is a handoff point, not completion of integration testing.

Keep the reviewed declaration and the reason for every manual change together. Otherwise, regenerating from an empty response next month can silently reintroduce the same weak array type that you corrected today.

Common questions

Is any[] proof that the API accepts arbitrary values?

No. In this generator it is the fallback for an empty sample array. It says nothing about the server's validation rules.

Can I use the Sample button as the API specification?

No. It loads demonstration data. Use it to learn the interface, then use the documented contract for your own service.

Does choosing type instead of interface fix null fields?

No. The value remains null in the input. Changing declaration style does not infer an unseen string value or optionality.

Will pasting several responses one after another merge them?

No. Each edit regenerates declarations from the current JSON input. Keep contrasting responses for manual review; the tool does not maintain a history of their combined field rules.

References