-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(posts): adds new 'ts-como-pegar-string-no-seu-literal-types' blo…
…g post
- Loading branch information
1 parent
9e62b84
commit a99db52
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
Binary file added
BIN
+3.79 KB
public/static/images/ts-como-pegar-string-no-seu-literal-types-small.jpg
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.