Skip to content

Instantly share code, notes, and snippets.

@develax
Last active September 20, 2024 21:16
Show Gist options
  • Save develax/d6a832cfaf89a512b8dd459c514477ef to your computer and use it in GitHub Desktop.
Save develax/d6a832cfaf89a512b8dd459c514477ef to your computer and use it in GitHub Desktop.

Infer Types

We have a type:

type Person = {
    name: string;
    age: number;
    hobbies: [string, string]; // tuple
}

A function that accepts an argument of an anonymous type where hobbies is a tuple:

function stringifyPerson(person: {
    name: string;
    age: number;
    hobbies: [string, string]; // tuple
}) {
    return `${person.name} is ${person.age} years old and loves ${person.hobbies.join(" and  ")}.`;
}

create a similar type:

const alex = {
    name: 'Alex',
    age: 20,
    hobbies: ['walking', 'cooking'] // here 'hobbies` is array, not tupple: string[] != [string, string]
}

trying to call the function with alex as argument will cause an error:

stringifyPerson(alex); // Type string[] is not assignable to type [string, string]

Let's declare type allias with tuple:

type Person = {
    name: string;
    age: number;
    hobbies: [string, string]; // tuple
}

and create another argument of this type explicitly:

const alex2: Person = {
    name: 'Alex',
    age: 20,
    hobbies: ['walking', 'cooking'] // now it is tuple = `[string, string]`
}

then call the function:

stringifyPerson(alex2); // now it's Ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment