{"id":3912,"date":"2016-04-30T01:27:36","date_gmt":"2016-04-30T01:27:36","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3912"},"modified":"2016-04-30T01:27:36","modified_gmt":"2016-04-30T01:27:36","slug":"typescript-typings-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-typings-example\/","title":{"rendered":"TypeScript: typings example"},"content":{"rendered":"<p>Typings is a command line utility that helps you find type bindings for various libraries. Basically, it works a lot like NPM, but it only manages the mappings from library -> type. If you&#8217;ve used C\/C++, this is like a Maven\/NPM for .H (header) files. <\/p>\n<p>Let&#8217;s take an example &#8211; first, we&#8217;ll install the utility:<\/p>\n<pre lang=\"bash\">\nnpm install -g typings\n<\/pre>\n<p>Then we can search for stuff:<\/p>\n<pre lang=\"bash\">\ntypings search react\n<\/pre>\n<p>Which apparently does a &#8220;contains&#8221; search:<\/p>\n<pre>\nNAME                              SOURCE HOMEPAGE                                           DESCRIPTION UPDATED                 \ngriddle-react                     npm    https:\/\/www.npmjs.com\/package\/griddle-react                    2016-04-13T18:36:51.000Z\nmobservable-react                 dt     https:\/\/github.com\/mweststrate\/mobservable-react               2016-03-16T15:55:26.000Z\nreact                             dt     http:\/\/facebook.github.io\/react\/                               2016-04-23T06:59:14.000Z\nreact-addons-create-fragment      dt     http:\/\/facebook.github.io\/react\/                               2016-03-16T15:55:26.000Z\nreact-addons-css-transition-group dt     http:\/\/facebook.github.io\/react\/                               2016-03-16T15:55:26.000Z\nreact-addons-linked-state-mixin   dt     http:\/\/facebook.github.io\/react\/                               2016-03-16T15:55:26.000Z\nreact-addons-perf                 dt     http:\/\/facebook.github.io\/react\/                               2016-03-16T15:55:26.000Z\nreact-addons-pure-render-mixin    dt     http:\/\/facebook.github.io\/react\/                               2016-03-16T15:55:26.000Z\nreact-addons-shallow-compare      dt     http:\/\/facebook.github.io\/react\/                               2016-03-16T15:55:26.000Z\nreact-addons-test-utils           dt     http:\/\/facebook.github.io\/react\/                               2016-04-17T14:49:49.000Z\nreact-addons-transition-group     dt     http:\/\/facebook.github.io\/react\/                               2016-04-17T13:41:18.000Z\nreact-addons-update               dt     http:\/\/facebook.github.io\/react\/                               2016-03-16T15:55:26.000Z\nreact-addons-update               npm    https:\/\/www.npmjs.com\/package\/react-addons-update              2016-03-24T04:43:46.000Z\nreact-bootstrap                   dt     https:\/\/github.com\/react-bootstrap\/react-bootstrap             2016-04-23T13:55:14.000Z\nreact-bootstrap-table             dt     https:\/\/github.com\/AllenFang\/react-bootstrap-table             2016-02-04T15:58:49.000Z\nreact-cropper                     dt     https:\/\/github.com\/roadmanfong\/react-cropper                   2016-04-03T14:21:19.000Z\nreact-datagrid                    dt     https:\/\/github.com\/zippyui\/react-datagrid.git                  2016-03-16T15:55:26.000Z\nreact-day-picker                  dt     https:\/\/github.com\/gpbl\/react-day-picker                       2016-03-16T15:55:26.000Z\nreact-dnd                         dt     https:\/\/github.com\/gaearon\/react-dnd                           2016-03-16T15:55:26.000Z\nreact-dom                         dt     http:\/\/facebook.github.io\/react\/                               2016-04-12T15:40:40.000Z\n<\/pre>\n<p>You can do an exact search by name, if you know what you want:<\/p>\n<pre lang=\"bash\">\ntypings search --name react\n<\/pre>\n<p>You can then install this by running this command (note &#8220;dt&#8221; is the source, it&#8217;s optional if the source is npm, and for bash you need the apostrophes)<\/p>\n<pre lang=\"bash\">\ntypings install 'dt!react' --ambient --save\n<\/pre>\n<p>Once this works, it makes a file called &#8220;typings.json&#8221;:<\/p>\n<pre lang=\"javascript\">\n{\n  \"dependencies\": {\n    \"react\": \"registry:dt\/react#0.14.0+20160423065914\"\n  }\n}\n<\/pre>\n<p>If you get a project from another developer, you may need to run this:<\/p>\n<pre lang=\"bash\">\ntypings install\n<\/pre>\n<p>This is also a good way to fix your local libraries if you end up with duplicates or things you don&#8217;t want (first, delete the &#8220;typings folder).<\/p>\n<p>To import Typings files, you can add a reference comment at the top of your ts\/tsx file:<\/p>\n<pre lang=\"javascript\">\n\/\/\/ <reference path=\"..\/typings\/main.d.ts\"\/>\n<\/pre>\n<p>Or include it as a line in tsconfig.json:<\/p>\n<pre lang=\"javascript\">\n{\n    \"compilerOptions\": {\n    \"module\": \"commonjs\",\n    \"target\": \"es5\",\n    \"jsx\": \"react\",\n    \"sourceMap\": true,\n    \"emitDecoratorMetadata\": true,\n    \"experimentalDecorators\": true,\n    \"outDir\": \"dist\"\n    },\n    \"exclude\": [\n      \"node_modules\",\n      \"dist\",\n      \"node_modules\",\n      \"typings\"\n    ],\n    \"files\": [\n      \".\/app\/index.tsx\",\n      \".\/typings\/main.d.ts\"\n    ]\n}\n<\/pre>\n<p>The &#8220;ambient&#8221; flag above is used to tell Typescript that the library you&#8217;re importing is some external one (&#8220;ambient&#8221; meaning part of the external environment).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All the things you need to get Typings up an running for the first time<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[302,388,557,558],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3912"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3912"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3912\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}