Skip to main content

Typescript

We've spent a lot of energy and time to make @pallad/config as much type-friendly as possible. There is a good chance that your entire configuration shape might be inferred by typescript automatically.

import {ResolvedConfig, type} from '@pallad/config';

export function createConfig() {
// return configuration shape
return {
mailer: {
email: env('EMAIL_FROM'),
smtp: {
port: env('EMAIL_SMTP_PORT', {transformer: type.int}),
password: env('EMAIL_SMTP_PASSWORD', {default: undefined})
}
}
}
}

export type Config = ResolvedConfig<ReturnType<typeof createConfig>>;

// typescript infers shape of config to
{
mailer: {
email: string;
smtp: {
port: number;
password: string | undefined
}
}
}

Then you can reuse the resolved configuration shape later

export function createMailer(config: Config['mailer']) {
config.mailer.email // string
config.smtp.password // string | undefined
}

Inferring problems

If for some reason config shape cannot be properly inferred then please ensure that default and/or transformer properties are properly typed.

export function createConfig() {
return {
ssl: {
// we cannot infer shape of privateKey
config: env('SSL_CONFIG', {transformer: JSON.parse})
}
}
}

const config = loadSync(createConfig());

// looks fine for typescript, but there is no such property
config.ssl.config.unknownProperty.foo;
function parseSSLConfig(config: string) {
const result = JSON.parse(config);
// parse and validate it

return {
privateKey: result.privateKey as string,
publicKey: result.publicKey as string
};
}
export function createConfig() {
return {
ssl: {
config: env('SSL_CONFIG', {transformer: JSON.parse})
}
}
}

const config = loadSync(createConfig());

// now it fails as it should
config.ssl.config.unknownProperty.foo;