(change) vs (ngModelChange) in angular

(change) event bound to classical input change event. You can use (change) event even if you don’t have a model at your input as (ngModelChange) is the @Output of ngModel directive. It fires when the model changes. You cannot use this event without ngModel directive. As you discover more in the source code, (ngModelChange) emits the new value. So it means … Read more

Interfaces vs Types in TypeScript

Original Answer (2016) As per the (now archived) TypeScript Language Specification: Unlike an interface declaration, which always introduces a named object type, a type alias declaration can introduce a name for any kind of type, including primitive, union, and intersection types. The specification goes on to mention: Interface types have many similarities to type aliases for object type literals, … Read more

Could not find a declaration file for module ‘module-name’. ‘/path/to/module-name.js’ implicitly has an ‘any’ type

That feeling when you are looking out for two days and find it like this: just remove .js from “main”: “dist/index.js” in package.json and everything works fine! UPD: this answer relative if you have your own npm package, if not – see my answer below. And if above answer not resolved import your module, try just add typings in package.json: Of course, here folder dist – it’s … Read more

TypeScript React.FC confusion

Function ComponentsThese can be written as normal functions that take a props argument and return a JSX element. What about React.FC/React.FunctionComponent? You can also write components with React.FunctionComponent (or the shorthand React.FC): Some differences from the “normal function” version: It provides typechecking and autocomplete for static properties like displayName, propTypes, and defaultProps – However, there are currently known issues using defaultProps with React.FunctionComponent. See this issue for details … Read more

How to convert a string to number in TypeScript?

Exactly like in JavaScript, you can use the parseInt or parseFloat functions, or simply use the unary + operator: All of the mentioned techniques will have correct typing and will correctly parse simple decimal integer strings like “123”, but will behave differently for various other, possibly expected, cases (like “123.45”) and corner cases (like null).  Table taken from this answer

How can I define an array of objects?

You are better off using a native array instead of an object literal with number-like properties, so that numbering (as well as numerous other array functions) are taken care of off-the-shelf. What you are looking for here is an inline interface definition for your array that defines every element in that array, whether initially present or introduced later: If … Read more