-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f27f96
commit 264f240
Showing
4 changed files
with
65 additions
and
4 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
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,28 @@ | ||
# typescript奇怪的代码 | ||
|
||
```typescript | ||
// 1:赋值 | ||
interface StringValidator { | ||
isAcceptable (s: string): boolean; | ||
} | ||
|
||
class LettersOnlyValidator implements StringValidator { | ||
isAcceptable (s: string) { | ||
return /^[0-9]+$/.test(s) | ||
} | ||
} | ||
|
||
let validators: { [s: string]: StringValidator } = {} | ||
validators['letters only'] = new LettersOnlyValidator() | ||
|
||
for (let name in validators) { | ||
let isMatch = validators[name].isAcceptable('23456') | ||
} | ||
|
||
// 2. 泛型套娃 | ||
type OrNull<T> = T | null | ||
type OneOrMany<T> = T | T[] | ||
// 右侧类似于OrNull<T | T[]>,即T | T[] | null | ||
type OneOrManyOrNull<T> = OrNull<OneOrMany<T>> | ||
|
||
``` |
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
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