Created
April 14, 2020 18:25
-
-
Save mateusduraes/cac065476c847c95ad1efbc931088acf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MUITO BOM! Parabéns pelo trampo e obrigado pelo esclarecimento