Skip to content

Commit

Permalink
feat(posts): adds new 'ts-como-pegar-string-no-seu-literal-types' blo…
Browse files Browse the repository at this point in the history
…g post
  • Loading branch information
rafa-thayto committed Jul 8, 2024
1 parent 9e62b84 commit a99db52
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions posts/ts-como-pegar-string-no-seu-literal-types.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: 'TypeScript - Como pegar uma string no seu Literal Types'
publishedTime: '2023-07-24T15:58:51Z'
modifiedTime: '2024-07-07T20:50:51Z'
description: 'Não vou enrolar nada. Basicamente é só você utilizar o Utility Type Extract do próprio TypeScript e boa!'
image:
{
src: 'ts-como-pegar-string-no-seu-literal-types.jpg',
placeholder: 'ts-como-pegar-string-no-seu-literal-types-small.jpg',
type: 'image/jpeg',
}
tags: ['typescript', 'utilities', 'types', 'tutorial']
href: '/blog/ts-como-pegar-string-no-seu-literal-types'
reactionsLength: 0
commentsLength: 0
---

Salve, salve devs seniors.

Não vou enrolar nada. Basicamente é só você utilizar o _Utility Type_ `Extract` do próprio **TypeScript** e boa! _**Por exemplo:**_

```ts
type CatNames = 'Garfield' | 'Yoru' | 'Yuki' | 'Fluffy' | 'Lady' | 'Lucky'

type MyCatNames = Extract<CatNames, 'Yoru' | 'Yuki'>
// |-> Extract these two `literal types` from CatNames `literal type`

type Cat = {
age: number
name: CatNames
}

function getMyCat(age: number, name: MyCatNames): Cat {
// |--> only show 'Yoru' and
// 'Yuki'
return {
age,
name,
} as Cat
}

const myCat1 = getMyCat(1, 'Yoru')
const myCat2 = getMyCat(1, 'Yuki')

const otherCat: Cat = {
age: 3,
name: 'Garfield',
}
const otherCatWithMyCatName: Cat = {
age: 5,
name: 'Yoru',
}

// tmj!
```

> link do [TS Playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBAwghsAcnAthAzlAvFA5AcTgCcAzASwgBsATXKAHzwE0B7IgVzsdyfYGsyXPADFK7EiRBDcAGTjUpDPDPYBjPlIBQm0JCgBZEPCSoM2KAFEAHsCJxVwADzHkadABpmbTkp79BAHyaUCGhYeERkVEA9NEMALQBljZ2DlDAABYY0MAA7ixQAAaUZMAQdpTp4BiFUCRELCiwCK5mxaXlcJW6EIXaPc3A5gDewVBwAOYQAFxQAHbsKABG5e5jc6azLqbomgC+2iTscw5kLHNQU8CGxgAUkzPziytEnhtoszctOwCUWwhQUZRYEgsKxBKJKDnSggKDoDIsXJeDh0OBzahQMag7ERcE4kJ+AS4MZECDAdhEC5A0IPNZhd4QOlQPbjTDGfbaVTndBDFBGBAARnMVy+wFuAs8PG8uB+mi5cx5UD5xgATMKyaLxZLeETZXLuUMWJlysZ-kMcNSQg9ZgBmJkhBmzAjEchUWgc+WKo1ZIjGADqpQyotaZpGWPGU1mAFZ7fNNsjOPsgA)
espero que ajude alguém!

(OBS: esse post foi escrito em no máximo 10 minutos)

meus links: https://thayto.com/linktree
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a99db52

Please sign in to comment.