Skip to content

Commit ea64dc7

Browse files
authored
Make functions generic so it can cast to opaque (branded) types (#521)
* Make functions generic so it can cast to opaque types * Add TypeScript section translations to ru, zh-CN and id-ID
1 parent 89f0ab4 commit ea64dc7

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed

README.id-ID.md

+23
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,29 @@ LZfXLFzPPR4NNrgjlWDxn
302302

303303
Bila ingin mengganti alfabet atau ukuran ID, dapat menggunakan [`nanoid-cli`](https://github.com/twhitbeck/nanoid-cli).
304304

305+
### TypeScript
306+
307+
Nano ID memungkinkan untuk mengubah string yang dihasilkan menjadi string opak
308+
dalam TypeScript. Sebagai contoh:
309+
310+
```ts
311+
type UserId = string & { [userIdBrand]: true }
312+
declare const userIdBrand: unique symbol
313+
314+
interface User {
315+
id: UserId
316+
name: string
317+
}
318+
319+
const user: User = {
320+
// Secara otomatis diubah menjadi UserId:
321+
id: nanoid(),
322+
name: 'Alice'
323+
}
324+
325+
// Gunakan parameter tipe secara eksplisit:
326+
mockUser(nanoid<UserId>())
327+
```
305328

306329
### Bahasa Pemrograman Lainnya
307330

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,29 @@ $ npx nanoid --alphabet abc --size 15
416416
bccbcabaabaccab
417417
```
418418

419+
### TypeScript
420+
421+
Nano ID allows casting generated strings into opaque strings in TypeScript.
422+
For example:
423+
424+
```ts
425+
type UserId = string & { [userIdBrand]: true }
426+
declare const userIdBrand: unique symbol
427+
428+
interface User {
429+
id: UserId
430+
name: string
431+
}
432+
433+
const user: User = {
434+
// Automatically casts to UserId:
435+
id: nanoid(),
436+
name: 'Alice'
437+
}
438+
439+
// Use explicit type parameter:
440+
mockUser(nanoid<UserId>())
441+
```
419442

420443
### Other Programming Languages
421444

README.ru.md

+24
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ $ npx nanoid --alphabet abc --size 15
396396
bccbcabaabaccab
397397
```
398398

399+
### TypeScript
400+
401+
Nano ID позволяет приводить сгенерированные строки к непрозрачным строкам в
402+
TypeScript. Например:
403+
404+
```ts
405+
type UserId = string & { [userIdBrand]: true }
406+
declare const userIdBrand: unique symbol
407+
408+
interface User {
409+
id: UserId
410+
name: string
411+
}
412+
413+
const user: User = {
414+
// Автоматически приводится к типу UserId:
415+
id: nanoid(),
416+
name: 'Alice'
417+
}
418+
419+
// Используйте явный параметр типа:
420+
mockUser(nanoid<UserId>())
421+
```
422+
399423
### Другие языки программирования
400424

401425
Nano ID был портирован на множество языков. Это полезно, чтобы сервер и клиент

README.zh-CN.md

+22
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,28 @@ $ npx nanoid --alphabet abc --size 15
370370
bccbcabaabaccab
371371
```
372372

373+
### TypeScript
374+
375+
Nano ID 允许将生成的字符串转换为 TypeScript 中的不透明字符串。 例如:
376+
377+
```ts
378+
type UserId = string & { [userIdBrand]: true }
379+
declare const userIdBrand: unique symbol
380+
381+
interface User {
382+
id: UserId
383+
name: string
384+
}
385+
386+
const user: User = {
387+
// 自动转换为 UserId:
388+
id: nanoid(),
389+
name: 'Alice'
390+
}
391+
392+
// 使用显式类型参数:
393+
mockUser(nanoid<UserId>())
394+
```
373395

374396
### 其他编程语言
375397

index.d.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
* ```
1111
*
1212
* @param size Size of the ID. The default size is 21.
13+
* @typeparam Type The type of the generated ID.
1314
* @returns A random string.
1415
*/
15-
export function nanoid(size?: number): string
16+
export function nanoid<Type extends string>(size?: number): Type
1617

1718
/**
1819
* Generate secure unique ID with custom alphabet.
@@ -22,6 +23,7 @@ export function nanoid(size?: number): string
2223
*
2324
* @param alphabet Alphabet used to generate the ID.
2425
* @param defaultSize Size of the ID. The default size is 21.
26+
* @typeparam Type The type of the generated ID.
2527
* @returns A random string generator.
2628
*
2729
* ```js
@@ -30,10 +32,10 @@ export function nanoid(size?: number): string
3032
* nanoid() //=> "8ё56а"
3133
* ```
3234
*/
33-
export function customAlphabet(
35+
export function customAlphabet<Type extends string>(
3436
alphabet: string,
3537
defaultSize?: number
36-
): (size?: number) => string
38+
): (size?: number) => Type
3739

3840
/**
3941
* Generate unique ID with custom random generator and alphabet.
@@ -58,13 +60,14 @@ export function customAlphabet(
5860
* @param alphabet Alphabet used to generate a random string.
5961
* @param size Size of the random string.
6062
* @param random A random bytes generator.
63+
* @typeparam Type The type of the generated ID.
6164
* @returns A random string generator.
6265
*/
63-
export function customRandom(
66+
export function customRandom<Type extends string>(
6467
alphabet: string,
6568
size: number,
6669
random: (bytes: number) => Uint8Array
67-
): () => string
70+
): () => Type
6871

6972
/**
7073
* URL safe symbols.

0 commit comments

Comments
 (0)