Skip to content

Commit

Permalink
fix: josa 리턴 타입 보강 (#275)
Browse files Browse the repository at this point in the history
* feat: josa 리턴 타입 보강

* Create gorgeous-shoes-check.md

---------

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
shcshcshc and okinawaa authored Nov 13, 2024
1 parent a49eeca commit 473a845
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-shoes-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": patch
---

fix: josa 리턴 타입 보강
14 changes: 8 additions & 6 deletions src/josa/josa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ type JosaOption =

const 로_조사: JosaOption[] = ['으로/로', '으로서/로서', '으로써/로써', '으로부터/로부터'];

export function josa(word: string, josa: JosaOption): string {
type ExtractJosaOption<T> = T extends `${infer A}/${infer B}` ? A | B : never;

export function josa<T extends string, U extends JosaOption>(word: T, josa: U): `${T}${ExtractJosaOption<U>}` {
if (word.length === 0) {
return word;
return word as `${T}${ExtractJosaOption<U>}`;
}

return word + josaPicker(word, josa);
return (word + josaPicker(word, josa)) as `${T}${ExtractJosaOption<U>}`;
}

josa.pick = josaPicker;

function josaPicker(word: string, josa: JosaOption): string {
function josaPicker<T extends JosaOption>(word: string, josa: T): ExtractJosaOption<T> {
if (word.length === 0) {
return josa.split('/')[0];
return josa.split('/')[0] as ExtractJosaOption<T>;
}

const has받침 = hasBatchim(word);
Expand All @@ -51,5 +53,5 @@ function josaPicker(word: string, josa: JosaOption): string {
index = 1;
}

return josa.split('/')[index];
return josa.split('/')[index] as ExtractJosaOption<T>;
}

0 comments on commit 473a845

Please sign in to comment.