When delving into the world of Typescript, you are not just learning a single language, but actually two distinct languages. The first language is Typescript itself, essentially Javascript with added type annotations and extensions like "enum" or defining "public/private" class members. The second language, let's call it Anders after its creator Anders Hejlsberg, focuses on the realm of types.
Anders serves the purpose of generating dynamic types for your program. While Typescript deals with manipulating values such as strings, numbers, objects, etc., Anders specializes in handling one specific data type: types themselves. In Anders, the values equate to types. An Anders function accepts type arguments and returns another type based on them.
Every instance of using <>
in your code signifies writing Anders code rather than typical Typescript code. This can be explicitly stated (as in MyType<T>
) or inferred through type inference mechanisms.
For illustration, consider this basic Typescript function that processes two values and outputs another:
function pair (x, y) {
return [x, y]
}
In contrast, an Anders function takes two types and yields another one derived from the input types:
type Pair<U, V> = [U, V]
In Typescript, supplying two values to pair
would result in an array composed of these values.
However, in Anders, giving Pair
the types number
and string
would generate [number, string]
- signifying a type encompassing all possible combinations of number,string
arrays such as [1, "hi"]
or [3.14, "hey"]
. Whereas providing it with string
and boolean
would produce the type characteristic of arrays like ["hi", true]
or ["blah", false]
.
Similar to other programming languages, Anders offers fundamental constructs focusing solely on operations related to types instead of actual values:
Built-in types include number
, string
, any
, {}
, akin to Typescript built-in objects like "Number" or "String".
Literals signify fixed values like "foo"
, representing not just any string but the exact string determined by its literal value.
Unions permit combining multiple types - similar to arrays in TS e.g., A|B|C
.
Structures act as mappings between types in Anders, analogous to objects mapping strings to values in TS. For example,
{foo: string; bar:number}["foo"]` ====> string
.
Operators like keyof
operator determine the keys within a type.
Comparisons primarily utilize the extends
keyword to ascertain subsets of types.
Conditionals, loops, function calls, and additional type checks complete the functionalities offered by Anders.
Returning to the simplified example provided:
// Code snippet cut for clarity
The function getContent
exemplifies an Anders function, accepting a type K
and returning a corresponding function type. By manually applying various types to this function, we can observe the outcomes dictated by the underlying type logic established by Anders.
At its core, Anders introduces developers to a unique paradigm where types take precedence over traditional value-focused coding approaches. Embracing Anders involves understanding the intricate dynamics imbued within type-driven programming, shedding light on a different perspective towards software development.
Exploring the boundaries set by Anders unlocks new possibilities and challenges, reshaping how we perceive and interact with programming structures at a foundational level.