4 ms·
I recently got into TypeScript, if that counts. I'm really enjoying it so far, but I'm running into some annoying things as a beginner: * For many third-party
by grardb 10y ago
I recently got into TypeScript, if that counts. I'm really enjoying it so far, but I'm running into some annoying things as a beginner:
* For many third-party libraries, needing to write `import * as Something from 'some-package'` instead of `import Something from 'some-package`.
* Not being able to import non-TypeScript files, further fragmenting my import style by needing to write stuff like `const styles = require('./something.scss')`.
* Module resolution in general seems to be close enough to ES6 that I'm usually okay, but different enough that I sometimes get really confused about why something doesn't work.
* There are tons of incorrect type definitions in the DefinitelyTyped repository. A common thing for me to do is: 1) `npm install --save-dev @types/some-package` 2) notice that my app no longer compiles because a method supposedly doesn't exist 3) `npm uninstall --save-dev @types/some-package` 4) move on with my life, because the method does indeed exist and my code works. Once I have more experience, I'd like to contribute to the repo to fix these errors, but the repository itself is GINORMOUS. 1k+ issues, 3k+ contributors, 25k+ commits, and 87 open pull requests right now. I have to think that there's a better way to manage types of third-party libraries.
* I find the docs very difficult to read.
If someone has a good resource for learning TypeScript, I'd love to know about it! Specifically, my pain points are module resolution (as you can see) and types beyond the basics. For example, I recently tried to make a React component where the props could be some custom things I wanted, PLUS all the fields of HTMLAnchorElement. I tried, but couldn't figure it out, and eventually switched the props type to `any`. This is less than ideal :(
- k__ 10y ago> For many third-party libraries, needing to write `import * as Something from 'some-package'` instead of `import Something from 'some-package`. I think this depends on the lib. As far as I know if a lib doesn't have a default export Babel is simply "emulating" one and TypeScript explicitly requires you to "export everything" > Not being able to import non-TypeScript files, further fragmenting my import style by needing to write stuff like `const styles = require('./something.scss')`. Yes this is a bit awkward, but I think it's bit cleaner than the stuff Babel etc. do. > There are tons of incorrect type definitions in the DefinitelyTyped repository. So true. I'm often forced to use require() to get around this. What helped me (but isn't for everyone) is choosing libs that are written in TypeScript in the first place like RxJS, Xstream, Cycle.js, Apollo-Client.
- jensvdh 10y agoThe Microsoft @types repository is actually not the recommended way of storing Typescript typings. Typescript developers are encouraged to package a Typescript definition file inside of their repository if possible. Typescript is smart enough to pick it up automatically. https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html https://www.typescriptlang.org/docs/handbook/declaration-fil...
- orif 10y agoFor packages with default export, you can use synthetic imports options in tsconfig. For non-js imports, you can create global module declarations (like the below snippet): declare module "*.css" { const value: any; export { value }; export default value; } But you will still need to use some bundler tool like webpack. It will just stop TypeScript from complaining. Type definitions are the biggest issue even today, but things are much better now. Most popular npm packages already have type definitions, but if you need some exotic package, then you can write your own defnitions (probably only necessary bits).
- grardb 10y agoOh nice, thanks for the tips! Those things will make my code so much nicer :D I am indeed using webpack, so that shouldn't be an issue (unless it doesn't work in conjunction with `target: 'node'`). Crossing my fingers!
- ng12 10y agoI have completely given up on DefinitelyTyped/Typings/@types. The odds that any non-trivial type definitions are complete, up-to-date, and correct seems to be incredibly slim -- and god help you if you're stuck on a very specific version of an NPM library. I had high hopes with @types but all I have to show for it is an increasingly large file containing modules declared as "any". Next time I'll go with Flow. Generally the only thing I REALLY need to be typed are the domain-specific models, and so I'd rather be as close to idiomatic JS/babel as possible.
- aashishkoirala 10y agoWhy not just ES6 with Babel?