TypeScript
Kosko and kubernetes-models are written in TypeScript, so you don't have to install any additional type declaration files.
Install
Install typescript, ts-node and @tsconfig/recommended. @tsconfig/recommended is optional, you can change it to any tsconfig you prefer.
- NPM
- Yarn
- PNPM
npm install typescript ts-node @tsconfig/recommended
--save-devyarn add typescript ts-node @tsconfig/recommended
--devpnpm install typescript ts-node @tsconfig/recommended
--save-devConfiguration
To start using TypeScript, you have to either add require option in kosko.toml, or run Kosko with -r/--require option.
require = ["ts-node/register"]
And then, create a tsconfig.json as below.
{
"extends": "@tsconfig/recommended/tsconfig.json",
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./typings"],
"moduleResolution": "node"
}
}
Environment Types
You can specify types of environment variables by extending type declarations of @kosko/env module.
import "@kosko/env";
// Extend type declarations of "@kosko/env" module
declare module "@kosko/env" {
// Declare types for global environment variables
interface GlobalEnvironment {
imageRegistry: string;
}
// Declare types for component environment variables
interface ComponentEnvironment {
// Fallback type of all other component variables which are not specified below
[key: string]: unknown;
// Specify types for each component
nginx: {
replicas: number;
};
}
// Extend Environment interface
interface Environment {
global(): GlobalEnvironment;
component<K extends string>(
name: K
): GlobalEnvironment & ComponentEnvironment[K];
}
}
Related
📄️ ECMAScript Modules
Kosko supports ECMAScript modules (ESM). You can write components and environments in native ESM files.
📄️ @kosko/env
@kosko/env package