4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TypeScriptで型クラスを書いたらどうなるか考えてみた

Posted at

PHPで型クラスを書くとどうなるか考えてみた - Qiitaに続いてちゃんとジェネリクスとコンパイルがある言語のTypeScriptで型クラスを書いたらどうなるのか考えてみた。TypeScriptでは3つめのbooleanなのにstringを渡している行がコンパイルエラーになった。

type Show<T> = (f: T) => string
const stringShow: Show<string> = (f: string) => f
const boolShow: Show<boolean> = (f: boolean) => f ? 'true' : 'false'
const quote = <T>(show: Show<T>) => (f: T) => '"' + show(f) + '"'

console.log(quote(stringShow)("hello")) //=> "hello"
console.log(quote(boolShow)(true)) //=> "true"
console.log(quote(boolShow)("hello")) //=> Argument of type '"hello"' is not assignable to parameter of type 'boolean'.
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?