Skip to content

Commit

Permalink
feat: integer
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhenpan committed Aug 5, 2024
1 parent 54bd0d3 commit 1edb611
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ type B = '' extends `${infer Head}${infer Tail}` ? 1 : 2
#### `Uncapitalize`会将给定字符串的第一个字符小写
#### 将数字类型的 1.0 转换为字符串,会得到 "1"
```ts
type NumberToString<T extends number> = `${T}`

type A1 = NumberToString<1.1>;
// type A1 = "1.1"

type A2 = NumberToString<1.0>
// type A2 = "1"
```
### never
#### 如何判断`never`
Expand Down
38 changes: 38 additions & 0 deletions src/10969-medium-integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
import { ExpectFalse, NotEqual } from './test-utils'

let x = 1
let y = 1 as const

type cases1 = [
Expect<Equal<Integer<1>, 1>>,
Expect<Equal<Integer<1.1>, never>>,
Expect<Equal<Integer<1.0>, 1>>,
Expect<Equal<Integer<1.0>, 1>>,
Expect<Equal<Integer<0.5>, never>>,
Expect<Equal<Integer<28.0>, 28>>,
Expect<Equal<Integer<28.101>, never>>,
Expect<Equal<Integer<typeof x>, never>>,
Expect<Equal<Integer<typeof y>, 1>>
]

// ============= Your Code Here =============
// type Integer<T extends number> = `${T}` extends `${infer F}.${infer R}`
// ? R[number] extends 0
// ? F
// : never
// : Equal<T, number> extends true
// ? never
// : T

type Integer<T extends string | number> = number extends T
? never
: `${T}` extends `${string}.${string}`
? never
: T

type NumberToString<T extends number> = `${T}`

type A1 = NumberToString<1.1>
type A2 = NumberToString<1.0>

0 comments on commit 1edb611

Please sign in to comment.