Last active
July 14, 2019 14:00
-
-
Save s2terminal/c2d412b05922e1595a69c25dbaaa2f41 to your computer and use it in GitHub Desktop.
TypeScriptの型で順序数を定義して、1+2が3であることをプログラムを動かさずに型定義で示す
This file contains hidden or 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
// http://www.typescriptlang.org/play/#code/PTAEHkCcBMEsDsCGAbUA5ArgWwEYFNJRo8AzBWAF1gHt4AoCgTwAc9QBJeAY0jyz3gUAPABUAfKAC8oANoiAugG4GLNuHhtpMpStagRAd2qgTUjt179BQ9XjHKmekQAtebM5x58Bww9Xu6bABi1BiEJtKelj6irnh2ynQgoADCtF4UbCQY3FS0dNm5NPCgzMgYAM62omIAFABuKABc+gCULVHe1uKgAN50JrwUYSUyjcg6AL50SWBpFniZoPDY+JB0XLQVFKC0eC22ZtrKm-DboBRGLX5mZZW2tXutiafnvBUYyDvSd1UatZdqM8NlsduMAILXIymaTvT4URSgZIAUUgkGo61eYJQACFrnFNKA4V8TqDQOMUi0QmEIkS8B8SUiwKj0esgA | |
// Ordinal Number definition | |
type Increment<T> = [T]; | |
type One = []; | |
type Two = Increment<One>; | |
type Three = Increment<Two>; | |
type Four = Increment<Three>; | |
// Concrete function | |
function plusOne<T>(val: T): Increment<T> { | |
return [val]; | |
} | |
// Concrete number | |
const one: One = []; | |
const two: Two = plusOne(one); | |
const result = plusOne(two); | |
const valA: Two = result; // Error | |
const valB: Three = result; | |
const valC: Four = result; // Error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment