Back to Type Challenges

README.Ja

questions/30970-medium-shitariteraru/README.ja.md

latest1.8 KB
Original Source
<!--info-header-start--><h1>確定した文字列リテラル </h1><blockquote><p>by 蛭子屋双六 <a href="https://github.com/sugoroku-y" target="_blank">@sugoroku-y</a></p></blockquote><p><a href="https://tsch.js.org/30970/play/ja" target="_blank"></a> &nbsp;&nbsp;&nbsp;<a href="./README.md" target="_blank"></a> </p><!--info-header-end-->

確定した文字列リテラル型かどうか、を判別したい場合があります。例えばクラスの識別子として指定された型が確定した文字列リテラル型であるか確認したいときです。

ts
type Action<ID extends string> = { readonly id: ID };

確定している必要があるので以下の型はfalseと判定する必要があります。

  • never型
  • 文字列リテラル型のUnion
  • string、number、bigint、booleanが埋め込まれたテンプレートリテラル型

与えられた型Sが確定した文字列リテラル型かどうかを判別して下さい

<!--info-footer-start-->

<a href="../../README.ja.md" target="_blank"></a> <a href="https://tsch.js.org/30970/answer/ja" target="_blank"></a> <a href="https://tsch.js.org/30970/solutions" target="_blank"></a> <!--info-footer-end-->