Skip to content

[wrangler] types --strict-vars=false emits invalid TypeScript ()[] for an empty array var #14886

Description

@chinesepowered

Which Cloudflare product(s) does this pertain to?

Wrangler core

What version(s) of the tool(s) are you using?

wrangler @ latest (verified against main at 960f199)

What version of Node is your environment running?

v20+

Describe the Bug

wrangler types --strict-vars=false emits ()[] — which is not valid TypeScript — for any var whose value is an empty array. Because this lands in the generated worker-configuration.d.ts, it does not just break that one line: the whole file fails to parse, so every binding type in the project is lost.

typeofArray derives the element types from the array's contents, so an empty array yields zero types and falls through to the join branch with an empty list:

https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/type-generation/index.ts#L2024-L2032

function typeofArray(array: unknown[]): string {
	const typesInArray = [...new Set(array.map((item) => typeof item))].sort();

	if (typesInArray.length === 1) {
		return `${typesInArray[0]}[]`;
	}

	return `(${typesInArray.join("|")})[]`;   // length === 0  ->  "()[]"
}

The length === 1 case is handled and the length >= 2 case is handled; length === 0 is not.

Steps to reproduce

With a wrangler.json containing an empty array var:

{
	"name": "my-worker",
	"compatibility_date": "2026-07-01",
	"vars": {
		"EMPTY_LIST": [],
		"MIXED": [1, "a"]
	}
}

Run:

npx wrangler types --strict-vars=false

Generated worker-configuration.d.ts contains:

EMPTY_LIST: ()[];          // <-- syntax error
MIXED: (number|string)[];

tsc then reports error TS1109: Expression expected. and no binding types resolve.

Expected behaviour

An empty array should produce a valid type. unknown[] is the natural choice, since nothing is known about the element type — never[] would also be valid but would make appends a type error for users who populate the var at runtime.

Please provide any relevant error logs

worker-configuration.d.ts(12,17): error TS1109: Expression expected.

I have a fix ready with a regression test and a changeset. Happy to open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:typesTypes issues to do with Wrangler type generationpackage:wranglerRelating to the `wrangler` package

    Type

    No type

    Projects

    Status
    Untriaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions