TypeScript ã§é¢æ°ã«ããããã£ãã¯ãããã
ãã®å 容㯠TypeScript 0.9.1 ããã¨ã«è¨è¿°ãã¦ãã¾ãã
TypeScript ã® 0.9.1.1 ã®ãã©ã³ããåããã¦ãã¦ãããããããç»å ´ããããªä»æ¥ãã®é ã§ãã
表é¡ã®ä»¶ã ç JavaScript ãªãçµæ§ãããã¨ãªã®ã§ããã TypeScript ã§ããã¨æããã¾ããä¾ãã°ä»¥ä¸ã®ãããªã³ã¼ããæ¸ããã¨ãã¦ã
function greeting(): string{ return "hello"; } greeting.name = function(): string{ return "k_maru"; }
ã³ã³ãã¤ã«ããã¨ã以ä¸ã®ããã«æããã¾ãã
error TS2094: The property 'name' does not exist on value of type '() => string'.
ãã¦ã以ä¸ã®ãããªã³ã¼ãã®æ¸ãæ¹ãããããã¨ãã¾ãã
//ãã£ãã·ã¥ãè¨å®ãã Lib.cache("user", { name: "hoge" }); //ãã£ãã·ã¥ããåå¾ var user = Lib.cache("user"); //ãã£ãã·ã¥ã®è¨å® Lib.cache.defaultDefinition({ maxSize: 10, expire: 10000 });
Lib.cache é¢æ°ã§ãã£ãã·ã¥ã®å¤ã®è¨å®ã¨åå¾ãããã¦ã¾ããã§ãLib.cache é¢æ°ããã¯ãã defaultDefinition é¢æ°ã§ãã£ãã·ã¥ã®è¨å®ãè¡ã£ã¦ã¾ããã¾ãããã¾ããçªã£è¾¼ã¿ã¯ç½®ãã¨ãã¦ãããããã³ã¼ãã§ããããå¤åã
ã§ãããã TypeScript ã§æ®éã«æ¸ãã¨ä»¥ä¸ã®ãããªæãã«ãªãã¨æãã®ã§ãããæåã®ä¾ã®éãã«æããã¦ãã¾ãã¾ãã
module Lib{ var items: {[index: string]: any;} = {}; export function cache(name: string, value?: any): any{ if(!value){ if(name in items){ return items[name]; } return; } items[name] = value; } cache.defaultDefinition = function(def: any): void{ //set definition } }
The property 'defaultDefinition' does not exist on value of type '(name: string, value?: any) => any'.
ã¾ããã¯ãããªããã®ã¯ä»æ¹ããªãã®ã§ããã TypeScript 0.9 ãã Declaration Merging ã£ã¦æ©è½ã§ module ãé¢æ°ã«ãããã¨ãã§ããããã«ãªã£ã¦ã¾ããã£ã¦ãã¨ã§ãæ©éã¤ãã£ã¦ã¿ã¾ãã
module Lib{ var items:{[index: string]: any;} = {}; export function cache(name:string, value?:any):any { if (!value) { if (name in items) { return items[name]; } return; } items[name] = value; } export module cache{ export function defaultDefinition(def:any):void { //set definition } } }
ããã§ç¡äºã«ã³ã³ãã¤ã«ãéã£ã¦ãæã£ããããªã³ã¼ãã®æ¸ããæ¹ãã§ããããã«ãªãã¾ããããã¹ã¦ã®å ´åã«ããã§å¯¾å¿ã§ãããã©ããã¯ãªãã£ã¦ãããé£ããæ°ããã¾ãããã¾ã OK ã§ãããã以ä¸ã®ãã㪠JavaScript ãã¯ããã¦ã¾ãã
var Lib; (function (Lib) { var items = {}; function cache(name, value) { if (!value) { if (name in items) { return items[name]; } return; } items[name] = value; } Lib.cache = cache; (function (cache) { function defaultDefinition(def) { //set definition } cache.defaultDefinition = defaultDefinition; })(Lib.cache || (Lib.cache = {})); var cache = Lib.cache; })(Lib || (Lib = {}));
ã£ã¦ãããã§çµãã£ããä¸ã æ³ãªãã§ãããããã¡ãã£ã¨è©±ãé²ãã¦ã¿ã¦ããã¾ã¯ cache é¢æ°ã§è¨å®ããã¦ããçã®å¤ãè¿ãã¦ã¾ããã çã®å¤ãä¿æãã CacheItem ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãè¿ãããã¨ãã¾ãããããçµæ§ããããã¾ããããå¤åã
以ä¸ã®ãããªã³ã¼ããæ¸ããããã¨ãã¾ãã
//ãã£ãã·ã¥ãè¨å®ããã¨ã CacheItem ãè¿ã£ã¦ãã var cacheItem = Lib.cache("user", { name: "hoge" }); //ãã£ãã·ã¥ãåå¾ããã¨ã CacheItem ãè¿ã£ã¦ãã cacheItem = Lib.cache("user"); //CacheItem ã«ã¯è²ã ããããã£ããã£ã¦ãç¶æ ã¨ãæ¡ä»¶ã確èªãããå¤æ´ãããã§ãã if(!cacheItem.expired){ cacheItem.expirationDateTime = new Date(now + (1000 * 60 * 60)); } //ãã¡ããå¤ãåãã var user = cacheItem.value(); Lib.cache.defaultDefinition({ maxSize: 10, expire: 10000 });
å®è£ ã¨ãã¦ã¯ cache é¢æ°ã¯ CacheItem ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãè¿ããããã©ãã·ã°ããã£ã¨ãã¦ã¯ã¤ã³ã¿ã¼ãã§ã¤ã¹ã§ãã ICacheItem ã¨ãããã CacheItem ã¯ã©ã¹ã¯å¤ããè¦ããªãããã«ãã¦ã new ãããããªãã£ã¦ã¨ããã§ãã ãªãã£ã¦ã JavaScript ã§ãnew ããã®ã£ã¦ãªããæ°æã¡æªãã£ã¦ã ãã§ããã
ããã«ã ICacheItem ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¯ Lib ã¢ã¸ã¥ã¼ã«ã®ä¸ã«ããããããªãã£ã¦ã Lib.cache ã¢ã¸ã¥ã¼ã«ã®ä¸ã«ããããã«ãã¾ãã Lib.cache ã¯é¢æ°ã§ããåæã«åå空éçãªã¢ã¸ã¥ã¼ã«ãªã®ã§ããã£ã¡ã®ã»ãããããã§ãã
module Lib{ var items:{[index: string]: cache.ICacheItem;} = {}; // ICacheItem ãè¿ã export function cache(name:string, value?:any): cache.ICacheItem { if (!value) { if (name in items) { return items[name]; } return; } // CacheItem ãä½ã var item = new CacheItem(value); items[name] = item; return item; } export module cache{ export function defaultDefinition(def:any):void { //set definition } export interface ICacheItem{ //properties } } // cache ã¢ã¸ã¥ã¼ã«ã®ä¸ã«ä½ãã¨ã cache é¢æ°ããè¦ããããã«ããããã«ã¯ // export ããªãã¨ã ãããã©ã export ããã¨å ¨ä½ã«è¦ãã¦ãã¾ããã // Lib ã¢ã¸ã¥ã¼ã«ã®ä¸ã«ä½ã class CacheItem implements cache.ICacheItem { constructor(public value: any){} //properties } }
CacheItem ã¯ã©ã¹ã Lib ã¢ã¸ã¥ã¼ã«ã®ä¸ã§ãå®è£ ãã¦ããã¤ã³ã¿ã¼ãã§ã¤ã¹ã® ICacheItem ã Lib ã¢ã¸ã¥ã¼ã«ã®ä¸ã® cache ã¢ã¸ã¥ã¼ã«ã£ã¦ã®ã§ãä¸ã®é層ãä¸ã®é層ãè¦ã¦ãã£ã¦ã®ã§æ°æã¡æªãã§ãããã¾ããä»æ¹ããªãã§ãã
ãããªãããªã§ãTypeScript ã®æã¯ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®èãæ¹ãã¡ãã£ã¨ JavaScript ã¨ã¯å¤ããå¿ è¦ãããããªãã¨æã£ããæããªãã£ãããTypeScript ã ãã§ãã¹ã¦æ¸ãããããã¤ä»ã® JavaScript ã©ã¤ãã©ãªã使ããªãã®ã§ããã°ã new ãæ°æã¡æªãã¨ããªãã¨æãã®ã§ããã©ããã¦ããã£ã±ãæ··ãã£ã¡ããã®ã§ããããããããå ´åã«ã§ããéãæ¸ããæ¹ã¯è¿ãã»ããããã¨èãã¦ã¦ãåããããã¨ããã¨ã TypeScript å´ã«ç¡çãåºã¦ãã¾ãã®ã§ãã©ã£ã¡ã«éããç½®ããã®éããªã®ã§ãããªããªãæ©ã¾ããã
æå¾ã¯ã ãã¶ã¿ã¤ãã«ãã話ãããããããªæ°ã¯ãã¾ããã以ä¸ã