Strange type error when testing TypeScript

6 days ago 6
ARTICLE AD BOX

A TypeError is shown when this empty test is run. The error only happens if the import statement is importing STATS and TSTATS.

test.ts

import { expect, test } from 'vitest' import { STATS, type TStats } from 'types' test('add stats', () => { // empty! }

types.ts

type TStats = [ number, number, number, number ] const STATS = { get ZERO():TStats { return [0, 0, 0, 0] }, get IDENTITY():TStats { return [1, 1, 1, 1]}, get BASE():TStats { return [ 0, 350, 0.95, 100]}, }

Somewhere else in the codebase this file exists components.ts:

export const CStats = { stats: new Array<TStats>( count).fill(STATS.BASE).map(() => STATS.BASE) }

The test above produces this error message:

TypeError: Cannot read properties of undefined (reading 'BASE') ❯ src/components.ts:76:49 export const CStats = { stats: new Array<TStats>(count).fill( STATS.BASE).map(() => {return STATS.BASE}) ^ }

I have no idea why this error message appears. The normal code runs without error. I tried it on TypeScript playground as well without any errors. I also added console.log messages in the map function before returning STATS.BASE. The code is executed.

The setup:

TypeScript 5.9.3

Vite 7.3.1

Vitest 4.0.16

Read Entire Article