SOLVED

Typings vs @types for SPFx - what is the guidence for referencing other javascript frameworks?

%3CLINGO-SUB%20id%3D%22lingo-sub-100976%22%20slang%3D%22en-US%22%3ETypings%20vs%20%40types%20for%20SPFx%20-%20what%20is%20the%20guidence%20for%20referencing%20other%20javascript%20frameworks%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-100976%22%20slang%3D%22en-US%22%3E%3CP%3EI%20was%20following%20a%20guide%20to%20implement%20handlebars%20in%20my%20SPFx%20web%20part%2C%20and%20it%20referenced%20using%26nbsp%3B%3C%2FP%3E%3CPRE%3Etypings%20install%20dt~handlebars%20--save%20--global%3C%2FPRE%3E%3CP%3E%26nbsp%3Bto%20add%20the%20handlebars%20reference%20in%20typescript.%20However%20the%20Typescript%20blog%20post%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Ftypescript%2F2016%2F06%2F15%2Fthe-future-of-declaration-files%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblogs.msdn.microsoft.com%2Ftypescript%2F2016%2F06%2F15%2Fthe-future-of-declaration-files%2F%3C%2FA%3E%20indicated%20that%20using%20the%20'%40types'%20approach%20is%20the%20recommended%20one%2C%20and%20even%20the%20'typings'%20github%20page%20has%20it%20marked%20as%20deprecated.%20However%20using%20this%20approach%20via%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Enpm%20install%20--save-dev%20%40types%2Fhandlebars%3C%2FPRE%3E%3CP%3Eproduces%20an%20error%20when%3C%2FP%3E%3CPRE%3Egulp%20serve%3C%2FPRE%3E%3CP%3Eor%3C%2FP%3E%3CPRE%3Egulp%20build%3C%2FPRE%3E%3CP%3Eis%20run%20after%20adding%26nbsp%3Bthe%26nbsp%3Bcode%20to%20import%20it%20into%20Typescript%20code%20via%20(for%20example).%3C%2FP%3E%3CPRE%3Eimport%20*%20as%20Handlebars%20from%20'handlebars'%3B%3C%2FPRE%3E%3CP%3EIs%20this%20a%20known%20issue%20with%20just%20Handlebars%3F%20Because%20i%20think%20I%20also%20hit%20a%20similar%20issue%20with%20jquery%20but%20am%20still%20investigating.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20is%20the%20official%20guidance%20for%20using%203rd%20party%20frameworks%3F%26nbsp%3BAre%20these%20transitional%20errors%20or%20is%20there%20something%20specific%20in%20the%20architecture%20of%20'%40types'%20that%20makes%20it%20incompatible%20and%20thus%20the%20need%20to%20still%20use%20'typings'%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-101204%22%20slang%3D%22en-US%22%3ERe%3A%20Typings%20vs%20%40types%20for%20SPFx%20-%20what%20is%20the%20guidence%20for%20referencing%20other%20javascript%20frameworks%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-101204%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3ETL%3BDR%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EUse%26nbsp%3B%40types...%20Typings%20is%20the%20older%20way%20of%26nbsp%3Bgetting%20type%20definitions%20prior%20to%20TypeScript%20v2.x.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EMore%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EDefinitely%26nbsp%3BTyped%20(%3CA%20href%3D%22http%3A%2F%2Fdefinitelytyped.org%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fdefinitelytyped.org%3C%2FA%3E)%20(DT)%20is%20the%20primary%20source%20for%20all%20type%26nbsp%3Bdefinitions%20when%20a%20package%20does%20not%20include%20the%20type%20definition%20within%20it%20(%3CEM%3Eas%26nbsp%3Bmore%20and%20more%20do%20these%20days...%20you%20can%20check%20by%20looking%20at%20the%20package.json%26nbsp%3Bof%20the%20NPM%26nbsp%3Bmodule%20you%20are%20importing...%20if%20there's%20a%20%22typings%22%20property%2C%20the%20library%20has%20%3C%2FEM%3Eits%3CEM%3E%20own%26nbsp%3Btype%20definition%3C%2FEM%3E).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EInitially%2C%20we%20used%20a%20tool%20called%20TSD%20to%20acquire%20and%20manage%20type%20definitions%20from%20DT.%26nbsp%3BThat%20was%20replaced%20by%20Typings.%20When%20TypeScript%202.x%20shipped%2C%26nbsp%3Bit%20introduced%20type%20definition%20management%20via%20scoped%20NPM%20packages...%20in%20this%20case%2C%26nbsp%3B%40types%20is%20the%20scope.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAt%20the%20same%20time%2C%20Microsoft%26nbsp%3Bcontributed%20a%20project%20(%3CA%20href%3D%22http%3A%2F%2Fmicrosoft.github.io%2FTypeSearch%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fmicrosoft.github.io%2FTypeSearch%3C%2FA%3E)%20that%20monitors%20additions%20%26amp%3B%26nbsp%3Bupdates%20to%20type%20def's%20in%26nbsp%3BDT.%20When%20it%20sees%20one%2C%20it%20pulls%20it%20down%20and%20creates%20%2F%20updates%20a%20new%20NPM%20package%20for%20that%20type%20definition%20(ex%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40types%2Fjquery%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40types%2Fjquery%3C%2FA%3E).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20way%2C%26nbsp%3Ba%20single%20NPM%20INSTALL%20command%20will%20get%20all%20the%20packages%20necessary%20%2B%20type%20definitions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESpecifically%20for%20your%20example%2C%20Handlebars%20doesn't%20export%20a%20top%20level%20object%20named%20%3CSTRONG%3EHandlebars%3C%2FSTRONG%3E...%20it's%20exported%20as%20a%20namespace...%20so%20you%20can%20just%20import%20the%20parts%20you%20are%20looking%20for.%20Like%3A%3C%2FP%3E%3CPRE%3Eimport%20%7B%20registerHelper%20%7D%20from%20'handlebars'%3B%0AregisterHelper(something%2C%20()%20%3D%26gt%3B%20%7B%20%7D%2C%20true)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3EOr%20you%20can%20alias%20the%26nbsp%3Bwhole%20thing%20like%20this%3A%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CPRE%3Eimport%20*%20as%20Handlebars%20from%20%22handlebars%22%3B%0AHandlebars.registerHelper(something%2C%20()%20%3D%26gt%3B%20%7B%20%7D%2C%20true)%3B%3C%2FPRE%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERef%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Ftypescript%2F2016%2F06%2F15%2Fthe-future-of-declaration-files%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblogs.msdn.microsoft.com%2Ftypescript%2F2016%2F06%2F15%2Fthe-future-of-declaration-files%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I was following a guide to implement handlebars in my SPFx web part, and it referenced using 

typings install dt~handlebars --save --global

 to add the handlebars reference in typescript. However the Typescript blog post at https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/ indicated that using the '@types' approach is the recommended one, and even the 'typings' github page has it marked as deprecated. However using this approach via

 

 

npm install --save-dev @types/handlebars

produces an error when

gulp serve

or

gulp build

is run after adding the code to import it into Typescript code via (for example).

import * as Handlebars from 'handlebars';

Is this a known issue with just Handlebars? Because i think I also hit a similar issue with jquery but am still investigating.

 

What is the official guidance for using 3rd party frameworks? Are these transitional errors or is there something specific in the architecture of '@types' that makes it incompatible and thus the need to still use 'typings'?

 

1 Reply
Highlighted
Solution

TL;DR

Use @types... Typings is the older way of getting type definitions prior to TypeScript v2.x.

 

More:

Definitely Typed (http://definitelytyped.org) (DT) is the primary source for all type definitions when a package does not include the type definition within it (as more and more do these days... you can check by looking at the package.json of the NPM module you are importing... if there's a "typings" property, the library has its own type definition).

 

Initially, we used a tool called TSD to acquire and manage type definitions from DT. That was replaced by Typings. When TypeScript 2.x shipped, it introduced type definition management via scoped NPM packages... in this case, @types is the scope.

 

At the same time, Microsoft contributed a project (http://microsoft.github.io/TypeSearch) that monitors additions & updates to type def's in DT. When it sees one, it pulls it down and creates / updates a new NPM package for that type definition (ex: https://www.npmjs.com/package/@types/jquery).

 

This way, a single NPM INSTALL command will get all the packages necessary + type definitions.

 

Specifically for your example, Handlebars doesn't export a top level object named Handlebars... it's exported as a namespace... so you can just import the parts you are looking for. Like:

import { registerHelper } from 'handlebars';
registerHelper(something, () => { }, true);

 

Or you can alias the whole thing like this:
import * as Handlebars from "handlebars";
Handlebars.registerHelper(something, () => { }, true);

 

Ref:

https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files