Skip to content

Instantly share code, notes, and snippets.

@mateusduraes
Created April 14, 2020 18:25
Show Gist options
  • Save mateusduraes/cac065476c847c95ad1efbc931088acf to your computer and use it in GitHub Desktop.
Save mateusduraes/cac065476c847c95ad1efbc931088acf to your computer and use it in GitHub Desktop.
interface Person {
name: string;
age: number;
extraInformation: {
salary: number;
role: string;
}
}
type Stringify<T> = {
/*
Conditional typing
Pra cada propriedade irá verificar se é objeto
* Se nāo for objeto, coloca o tipo como string
* Se for objeto, a tipagem do objeto será mapeada pelo stringify
Repare que se torna algo muito parecido com uma recursividade
*/
[P in keyof T]: T[P] extends object ? Stringify<T[P]> : string
}
const person: Person = {
name: 'Mateus',
age: 25, // number
extraInformation: {
salary: 100, // number
role: 'Developer'
}
}
const personStr: Stringify<Person> = {
name: 'Mateus',
age: '25', // string
extraInformation: {
salary: '100', // string
role: 'Developer'
}
}
@houstondapaz
Copy link

MUITO BOM! Parabéns pelo trampo e obrigado pelo esclarecimento

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment