These are chat archives for AngularClass/angular2-webpack-starter

7th
Mar 2016
Joanne Yae
@nyjy85
Mar 07 2016 17:53
How would I be able to load an external library such as moment or lodash with this starter?
This has been the biggest struggle so far in learning angular 2
This file at the bottom explains that you can just import them wherever in the file
if they have “imports” or whatever
then to make them “Typescripty” you will need to install a type definition
which is basically like “jquery can do this thing, with this object"
MOST of the time you can find a type definition by just running typings install jQuery —ambient —save
but sometimes you will need to make it custom
THERE ARE SOME LIBRARIES like Google Maps API that require you import them in an HTML file
I needed to do this. Basically. Just include it as a html script tag like you usually would.
then you will need type definitions. I found some for the maps api here https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/googlemaps/google.maps.d.ts
after I install that I could reference google.maps anywhere in my Typescript
@nyjy85 Let me know if that helps or if I can answer any other questions.
Joanne Yae
@nyjy85
Mar 07 2016 19:01
@Spittal So I did "import * as moment from 'moment'" in one of my .ts files. It loads the library (i checked with a console log) but an error gets thrown in console "/home/home.ts
(4,25): error TS2307: Cannot find module 'moment'." even though it's loaded.
it doesnt prevent the app from loading but why does it throw an error, even though its loaded?
Jamie Spittal
@Spittal
Mar 07 2016 19:02
Did you get the moment typings?
Joanne Yae
@nyjy85
Mar 07 2016 19:02
do i need it?
Jamie Spittal
@Spittal
Mar 07 2016 19:02
Yeah, you see that it’s TS2307 error
Joanne Yae
@nyjy85
Mar 07 2016 19:02
Do I need a typings for every 3rd party module i want to load?
Jamie Spittal
@Spittal
Mar 07 2016 19:02
so that’s your Typescript compiler complaining
Yes, but you can choose to be lazy and just tell typescript to more or less ignore the library
Joanne Yae
@nyjy85
Mar 07 2016 19:03
ooo ok i get it. I see now
Damn, that was so much help
Jamie Spittal
@Spittal
Mar 07 2016 19:03
but that’s not taking advantage of what typescript brings to the table.
Joanne Yae
@nyjy85
Mar 07 2016 19:03
so the best practice is to have a typings for everything pretty much
Jamie Spittal
@Spittal
Mar 07 2016 19:04
That’s correct
Joanne Yae
@nyjy85
Mar 07 2016 19:04
and that file itself is basically just declaring variables and its data type?
Jamie Spittal
@Spittal
Mar 07 2016 19:04
Correct.
typings install --save --ambient moment
That will install the typings
after you do that typescript will stop yelling at you
You can see what the file does here
Joanne Yae
@nyjy85
Mar 07 2016 19:05
it's throwing me a "Cannot find namespace 'moment'"
Jamie Spittal
@Spittal
Mar 07 2016 19:05
oh uhhhh
one sec..
Yeah I’m getting that too
so run typings uninstall --save --ambient moment
then try typings install —save moment
(just the same without the ambient flag)
I’m not sure if it will work
but worth a try
and if it doesn’t work, well I have more things up my sleeve
Joanne Yae
@nyjy85
Mar 07 2016 19:08
i go this error ' Unable to find "—-save" in the registry. Did you want to install an ambient typing? Try using "--ambient".'
Jamie Spittal
@Spittal
Mar 07 2016 19:08
hahaha
Yeah apparently moment is a little odd how it works..
found this.
They aren’t using typings they are using the older tsd but more or less they are the same.
Maybe you can find some help there
Joanne Yae
@nyjy85
Mar 07 2016 19:10
Ohhh ok I see. Thank you!! So this will be an issue if a module returns a function as opposed to an object? Hopefully other 3rd party typings arent as annoying as moment
Jamie Spittal
@Spittal
Mar 07 2016 19:10
Yeah, I haven’t had much trouble with any other typings
Took me a little while to understand how and why to use them though
Growing pains with ng2.
But I really love the features Typescript gives. I come from a Java world
so strongly typed = <3
You also can expect as Angular2 raises in popularity the typings will get 1000x more reliable.
Jamie Spittal
@Spittal
Mar 07 2016 19:19
Plus I think it’s getting pretty popular to write React in TS now too.
Joanne Yae
@nyjy85
Mar 07 2016 19:19
@Spittal You have been such amazing help. I've been banging my head on this for days. Thank you so much!!!!
errr really?
Jamie Spittal
@Spittal
Mar 07 2016 19:19
Well… TSX
Joanne Yae
@nyjy85
Mar 07 2016 19:20
I get why people like typescript and i want to like it. I'm slowing starting to appreciate it
Jamie Spittal
@Spittal
Mar 07 2016 19:21
Yeah, it’s a little more writing forsure. But once you start really taking advantage of interfaces, abstract classes, super classes you will really see the benefit.
For example I have an abstract class for a component that animates a background. I use that as a super class for several other components in my app. Using normal JS that would be such a hassle.
but now it’s dream