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( count).fill(STATS.BASE).map(() => STATS.BASE)
}
... and 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(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.
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]},
}
const CBaseStats = {
stats: new Array(5).fill( STATS.BASE).map(() => {return STATS.BASE})
}
Any hints where to check are welcome.
The setup: Typescript 5.9.3
vite@7.3.1
vitest@4.0.16