Which method is the better choice?
@Injectable({
providedIn: 'root'
})
export class MyService {
static readonly VALIDITIES = new Map<number, string>([
...
]);
...
}
OR:
const Validities = new Map<number, string>([
...
]);
@Injectable({
providedIn: 'root'
})
export class MyService {}
I have mostly used the first approach before, but encountered an error when trying to combine two Maps. Interestingly, the error only occurs in my jasmine unit tests!
@Injectable({
providedIn: 'root'
})
export class MyService {
static readonly VALIDITIES = new Map<number, string>([
[1, 'A'],
[2, 'B'],
[3, 'C'],
[4, 'D']
]);
static readonly VALIDITY_FILTERS = new Map<number, string>([
[0, 'invalid'],
...Array.from(MyService.VALIDITIES.entries()), // XXXX
[99, 'all']
]);
...
}
Error message in line (XXXX):
An error was thrown in afterAll
Uncaught TypeError: Cannot read properties of undefined (reading 'VALIDITIES')
TypeError: Cannot read properties of undefined (reading 'VALIDITIES')
What could be causing this issue in the code? Is there an alternative solution available?