文字列が適切か考える
はじめに
初心者向けです。
初心者にありがちなことと、TSに入りたてでありがちなことを書きました。
文字列が適切でない選択である可能性を意識する
文字列使いがち問題
自分が以前見かけたコードに下記のようなものがありました。
Next.jsにも例としてこのようなコードが載っています(このコード自体は問題ないです)。
<Link href="/about"><a>About</a></Link>
このコード単体で見ると、問題ないですが、この書き方をそのままプロダクションでも行われるとすぐに面倒なことになります。
自分が問題視しているのは、/about
の部分です。
自分が見たコードは、全て(どこでも)hrefの中身が文字列で書かれていました。そのため、タイポっていても実行時にしかわからず(どの部分でも実際にリンク踏むまで正しいかわからない)、再利用性はなく、TypeScriptを利用しているにも関わらず、型の恩恵も受けられない部分になっていました。
本来は、
下記のようにパスを定義しておき、
const aboutPath = () => '/about'
下記のように定義したパスを使うなどすべきです。
<Link href={aboutPath()}><a>About</a></Link>
こうすることにより、型の恩恵を受けられ、タイポすることもなくなります。
* 前提として(当然ながら)リンクはどこからでも使われるというのがあります。一箇所でしか使わないならそのまま文字列を直接使っても良いかもしれませんが、整合性の観点からは全て、関数や定数にすべきだと思います(特にパスの場合は、idなどを埋め込むことになると思うので関数にすべき)。
string型使いがち
使用するものが決まっている場合には、enumを使うべき(TypeScriptならunion typeでも一応可)
// enum版
enum Color {
red = 'red',
green = 'gleen',
blue = 'blue'
}
const printColor = (color: Color) => {
console.log(color)
}
printColor(Color.blue)
// union type版
const printColor = (color: 'red' | 'green' | 'blue') => {
console.log(color)
}
printColor('red')
おまけ
下記は、最近見かけたAPIです。
withZone(ZoneId.of("UTC"))
上よりも下のAPIのが良いです。
withZone(DateTimeZone.UTC)
Some bonuses use a mechanical device, corresponding to a spinning wheel, that works along side of} the bonus to display the amount won. As for the slot machines, I would take movies of how the reels would go into hyper-speed to keep away from the bonus symbol it might, and will, have landed on. Just like another on line casino sport, slots supply a risk to win actual cash. No one can assure you wins end result of|as a result of} slots are a sport of likelihood, but find a way to|you probably can} certainly get an higher hand if you use the winning slot suggestions from this article. Always keep this in thoughts, particularly if you love half in} slots with progressive jackpots. Many games give you access to the top wins only whenever you bet the maximum — and that is why a correct slot strategy and bankroll strategy are essential to have an opportunity to winning 카지노사이트.online at slots.
返信削除Early computerized slot machines were sometimes defrauded through utilization of} dishonest gadgets, such as the "slider", "monkey paw", 1xbet "lightwand" and "the tongue". Many of these old dishonest gadgets were made by the late Tommy Glenn Carmichael, a slot machine fraudster who reportedly stole over $5 million. In the fashionable day, computerized slot machines are fully deterministic and thus outcomes can be sometimes efficiently predicted.
返信削除