Rook
РусскийRU

make

make(T) is the value of a type, which the compiler computes, so no type is left in the JS: make(Song) is { title: "", seconds: 0 }, make(Song[]) is [], make(Seconds) of type Seconds number is 0. For the board, make(Task[]) creates an empty, typed task list; it does not populate the list or initialize any additional store behavior.

An interface gives an object of its fields, each with its own value; a field that may be missing, ?T or any, is left out. A literal type gives its literal, Map and Set are empty, and a class whose constructor takes nothing is new Cart(). Each make is a new value. A union, a function, an interface with methods and a type that is its own field have no value of their own: the compiler says why.

In a generic function make(T) is the value of the type of each call, fill<Song>(2) or one inferred from the arguments. A function called with one type gets the value in its body; one called with several, or exported, takes a function that makes it before its own parameters; one never called is left out of the JS with a warning. decode<T> takes its type the same way (decode).

// make(T) is the value of a type, computed when the program compiles: no type is left in the JS
interface Song {
    title string
    seconds number
    tags string[]
    album ?string // may be missing, so it is left out
}

type Seconds number

const blank = make(Song)      // { title: "", seconds: 0, tags: [] }
const songs = make(Song[])    // []
const pause = make(Seconds)   // 0

// In a generic function T comes from each call: the type in <> or from the arguments
func fill<T>(n number) T[] {
    const out T[] = []
    for i in 0..n {
        out.push(make(T)) // a new value each time
    }
    return out
}

const drafts = fill<Song>(2)
const zeros = fill<number>(3)
drafts[0].title = "Intro"
console.log(blank, songs, pause)
console.log(drafts, zeros)
// Called with two types, fill takes a function that makes the value: fill(() => 0, 3)