オブジェクトから型を生成する
多くの言語では型による構造体、オブジェクトの定義をしてからコーディングが始まりますが、元がJavaScriptであるTypeScriptにはそのような決まりがないことも多々あります。
一般的な型を先に決めるプログラミング
多くの言語ではその型が何かを決めてから、その型に属するオブジェクトを決めます。次の例はTypeScriptの例ですが、他の言語に当てはめても問題なく受け入れられると思います。
tstypeAccount = {accountName : string;password : string;age : number;plan : "Free" | "Standard" | "Premium";};constaccount :Account = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard",};
tstypeAccount = {accountName : string;password : string;age : number;plan : "Free" | "Standard" | "Premium";};constaccount :Account = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard",};
すでにJavaScriptの資産があるプロジェクトにおいては表立って型などなく、そのためAccountといった型は存在せず代入式のconst accountのみが存在していることでしょう。そんなときはこのconst accountをTypeScriptに変換してできるだけ近い形で型を作ることができます。
typeof
このtypeofはJavaScriptのものではなく、TypeScriptのtypeofです。これを実際に動作している変数に使ってみるとその変数をTypeScriptはどのような型と認識しているのかがわかります。
tsconstaccount = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard",};typeAccount = typeofaccount ;
tsconstaccount = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard",};typeAccount = typeofaccount ;
planが意図するユニオン型にはなりませんが、それなりに近い型を得ることができました。
プロパティを定数値で取得したい場合
プロパティを定数値で取得したい場合はオブジェクトにas constをつけます。
tsconstaccount = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard",} asconst ;typeAccount = typeofaccount ;
tsconstaccount = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard",} asconst ;typeAccount = typeofaccount ;
特定のプロパティだけを定数値で取得したい場合
これでは型の制約が強力すぎて他の値が代入できないので、もう少し柔軟にします。たとえばplanだけがユニオン型になるようにしたければplanの右に希望の型を書いてあげればそれでその型になります。
tsconstaccount = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard" as "Free" | "Standard" | "Premium",};typeAccount = typeofaccount ;
tsconstaccount = {accountName : "yyts",password : "ccbyncsa30",age : 80,plan : "Standard" as "Free" | "Standard" | "Premium",};typeAccount = typeofaccount ;