Skip to main content

TypeScript

Kosko and kubernetes-models are written in TypeScript, so you don't have to install any additional type declaration files.

Examples

Install

Install typescript, ts-node and @tsconfig/recommended. @tsconfig/recommended is optional, you can change it to any tsconfig you prefer.

npm install 

typescript ts-node @tsconfig/recommended

--save-dev

Configuration

To start using TypeScript, you have to either add require option in kosko.toml, or run Kosko with -r/--require option.

kosko.toml
require = ["ts-node/register"]

And then, create a tsconfig.json as below.

tsconfig.json
{
"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.

typings/@kosko__env/index.d.ts
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];
}
}