TypeScript & Types
SVDM is battery-included with TypeScript support. This means that you can use SVDM with TypeScript to enforce schema and type checking without any configuration, and it is designed to work with TypeScript for its full potential.
This document will guide you through what you can expect from SVDM when messing up with the types, and how our typing system works.
Schema Enforcement
Let’s get started with a very simple example. We have one variant called variant
and it has two variations: primary
and secondary
.
In this example, if we change the value of the secondary variance to a number, it will throw a TypeScript typing error that warns you about the type mismatch.
Same thing happens for nested objects as well. By default, we expect that all the variations of a variant should have the same type. If you try to assign a different type to a variation, TypeScript will throw an error.
Customized Singleton Typing
In some cases, you may want to have a optional types for a specific variation. You can achieve this by defining a custom type for that variation, so it won’t automatically infer from the definition.
With this approach, you can define a custom type for a specific variation, and it will be enforced by TypeScript. The benefit? You don’t have to define the framework of enforcing a config object to obey the type schema. SVDM will do it for you in a more concise way.