tags : TypeScript, fp-ts
Links
-
オフィシャル
-
参考記事
Tips
union 型を使うときの IE11 のバグ
ネストしたオブジェクトとnullのunionの場合、先にnullを記述しないとエラーになる
// IE11 で
// 未定義または null 参照のプロパティ sourceUrl
// エラーになる
image: t.union([
t.type({
sourceUrl: t.string
}),
t.null,
])// IE11でもエラーにならない
image: t.union([
t.null,
t.type({
sourceUrl: t.string
}),
])union 型のチェック
const colors = {
black: "#000000",
white: "#ffffff",
red: "#ff0000"
}
type ColorKey = keyof typeof colors
const isColorKey = (key: string): key is ColorKey => {
return Object.keys(colors).some((value) => value === key)
}
const ColorKeysDecorder = pipe(
D.string,
D.parse((s) => {
return isColorKey(s) ? D.success(s) : D.failure(s, 'ColorKey')
}),
),