Within my config.test.json
, the contents are as follows:
{
"development": {
"username": "user1",
"database": "db1"
},
"production": {
"username": "user2",
"database": "db2"
}
},
"SEED": true
In the file index.ts
, there is an interface defined as:
interface IConfigDB {
username: string;
database: string;
otherProp: number;
}
The actual object configDB
looks like this:
const configDB: IConfigDB = {
...config[process.env.NODE_ENV],
otherProp: false,
};
Despite setting otherProp
to false
, which should actually be a number, TypeScript does not raise any errors. However, when testing on the TypeScript sandbox by mocking config.test.json
as an object, it works correctly — you can see the results in the sandbox.
const development = {
username: "user1",
database: "db1"
};
interface IObjectA {
username: string;
database: string;
otherProp: boolean;
}
const configDB: IObjectA = {
...development,
otherProp: 1
};
// (property) IObjectA.otherProp: boolean
// Type 'number' is not assignable to type 'boolean'.
// Quick Fix...
// Peek Problem
This is how my tsconfig.json
file is configured:
{
"compilerOptions": {
"outDir": "./dist/",
"target": "es5",
"allowJs": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": ["src"],
"exclude": ["node_modules"]
}
Can someone provide insights into what might be causing this behavior?
Update:
Upon adding "strict": true
to the tsconfig.json
, an error occurs when attempting to set
const configDB: IConfigDB = {
...config[process.env.NODE_ENV],
operatorsAliases: false,
};
The error message reads:
(property) NodeJS.Process.env: NodeJS.ProcessEnv
Type 'undefined' cannot be used as an index type.ts(2538)
Any suggestions to address this issue?
-- Update 2:
After @lukasgeiter pointed out that the type for ...config[process.env.NODE_ENV]
is string | undefined
, using !
will inform TypeScript that there is indeed a value present...
However, this action leads to an error on:
...config[process.env.NODE_ENV!],
The corresponding error message is displayed https://i.sstatic.net/TCn0r.png
It seems to suggest something related to the absence of types within my config.test.json
. Further clarification would be appreciated.
Unclear about what the error signifies... Do you have any insights?