Table of Contents
Disclaimer
Type Declarations
View Transition
The startViewTransition
function is a method on the document
object which enables view transitions. The following types are based on the documentation at MDN. Since the API is currently experimental, you may need to add the following type to your type declaration file to avoid errors.
Using in SvelteKit
In SvelteKit, you can add the above type declaration to a file in the src
directory, such as src/types.d.ts
. Then you can add view transitions to your SvelteKit app by following the video below, ignoring the @ts-ignore
comments.
Environment Variables
Environment variables can be declared in the global
namespace to provide type information for import.meta.env
and process.env
. The following example demonstrates how to declare environment variables in TypeScript.
Keep in mind that this is type faith, not type safe. You can also use a schema validation library to parse your environment variables and infer the type from that. If a variable is missing, you can throw an error.
Utility Types
Combine/Prettify Two Object Types
The Prettify
type is a simple type which takes an object type with intersections and combines them to make them easier to read. The Combine
type is a simple type which takes 2 object types and combines them into one.
Prettifying Nested Objects
Going even further, you can use the PrettifyNested
type to prettify nested object types. The Record<PropertyKey, unknown>
type extends basic objects, but not classes like Date
, Map
, Set
, etc.
Numeric Range
The NumericRange
type is a utility type that takes a start and end number and returns a union of all numbers between the two.
Type Guards
IsAny
There are a few ways to check if a type if any. One example is to use an interection. As explained in the source link below:
The type constraint
0 extends 1
is not satisfied (0
is not assignable to1
). Therefore, it should be impossible for0 extends (1 & T)
to be satisfied either, since(1 & T)
should be even narrower than1
. However, whenT
isany
, it reduces to0 extends any
, which is satisfied. That’s becauseany
is intentionally unsound and acts as both a supertype and subtype of almost every other type (except fornever
).
Another method is to use the fact that it’s both a subtype and supertype of almost every other type by extending never
. When any extends never
, it results in a union of true | false
or boolean
. boolean
extends boolean
, but not true
or false
individually.
Satisfies
The Satisfies
type is a utility type that checks if a type satisfies an expected type and returns the actual type.