Skip to content
Myr

Make functions generic so it can cast to opaque (branded) types (#521)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Make functions generic so it can cast to opaque types * Add TypeScript section translations to ru, zh-CN and id-ID

authored by Sasha Koss · committed by GitHub · parent 89f0ab4 · Browse files · 5 files+100 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+100 added−5 removed

  • other:+100 added−5 removed

5 files+100 added−5 removed

modified:index.d.ts+8 added-5 removed
modified:README.id-ID.md+23 added
300 unmodified lines
301```302303Bila ingin mengganti alfabet atau ukuran ID, dapat menggunakan [`nanoid-cli`](https://github.com/twhitbeck/nanoid-cli).304305### TypeScript306307Nano ID memungkinkan untuk mengubah string yang dihasilkan menjadi string opak308dalam TypeScript. Sebagai contoh:309310```ts311type UserId = string & { [userIdBrand]: true }312declare const userIdBrand: unique symbol313314interface User {315 id: UserId316 name: string317}318319const user: User = {320 // Secara otomatis diubah menjadi UserId:321 id: nanoid(),322 name: 'Alice'323}324325// Gunakan parameter tipe secara eksplisit:326mockUser(nanoid<UserId>())327```328329### Bahasa Pemrograman Lainnya330331Nano ID telah bermigrasi ke berbagai macam bahasa. Seluruh versi dapat digunakan untuk mendapatkan ID generator yang sama pada sisi klien dan sisi penyedia layanan (_client-side_ dan _server-side_).332
39 unmodified lines
modified:README.md+23 added
414 unmodified lines
415$ npx nanoid --alphabet abc --size 15416bccbcabaabaccab417```418419### TypeScript420421Nano ID allows casting generated strings into opaque strings in TypeScript.422For example:423424```ts425type UserId = string & { [userIdBrand]: true }426declare const userIdBrand: unique symbol427428interface User {429 id: UserId430 name: string431}432433const user: User = {434 // Automatically casts to UserId:435 id: nanoid(),436 name: 'Alice'437}438439// Use explicit type parameter:440mockUser(nanoid<UserId>())441```442443### Other Programming Languages444445Nano ID was ported to many languages. You can use these ports to have446the same ID generator on the client and server side.
51 unmodified lines
modified:README.ru.md+24 added
394 unmodified lines
395$ npx nanoid --alphabet abc --size 15396bccbcabaabaccab397```398399### TypeScript400401Nano ID позволяет приводить сгенерированные строки к непрозрачным строкам в402TypeScript. Например:403404```ts405type UserId = string & { [userIdBrand]: true }406declare const userIdBrand: unique symbol407408interface User {409 id: UserId410 name: string411}412413const user: User = {414 // Автоматически приводится к типу UserId:415 id: nanoid(),416 name: 'Alice'417}418419// Используйте явный параметр типа:420mockUser(nanoid<UserId>())421```422423### Другие языки программирования424425Nano ID был портирован на множество языков. Это полезно, чтобы сервер и клиент426генерировали ID по одной схеме.
48 unmodified lines
modified:README.zh-CN.md+22 added
368 unmodified lines
369$ npx nanoid --alphabet abc --size 15370bccbcabaabaccab371```372373### TypeScript374375Nano ID 允许将生成的字符串转换为 TypeScript 中的不透明字符串。 例如:376377```ts378type UserId = string & { [userIdBrand]: true }379declare const userIdBrand: unique symbol380381interface User {382 id: UserId383 name: string384}385386const user: User = {387 // 自动转换为 UserId:388 id: nanoid(),389 name: 'Alice'390}391392// 使用显式类型参数:393mockUser(nanoid<UserId>())394```395396### 其他编程语言397398Nano ID 已被移植到许多语言。 你可以使用下面这些移植,获取在客户端和服务器端相同的ID生成器。399
47 unmodified lines