concept type in category angular

appears as: types, types, Type, type, The type
Angular Development with Typescript, Second Edition

This is an excerpt from Manning's book Angular Development with Typescript, Second Edition.

The reason is that developing in JavaScript isn’t overly productive. Say a function expects a string value as an argument, but the developer mistakenly invokes it by passing a numeric value. With JavaScript, this error can be caught only at runtime. Java or C# compilers won’t even compile code that has mismatching types, but JavaScript is a dynamically typed language and the type of a variable can be changed during runtime.

Although JavaScript engines do a decent job of guessing the types of variables by their values, development tools have a limited ability to help you without knowing the types. In mid- and large-size applications, this JavaScript shortcoming lowers the productivity of software developers.

On larger projects, good IDE context-sensitive help and support for refactoring are important. Renaming all occurrences of a variable or function name in statically typed languages is done by IDEs in a split second, but this isn’t the case in JavaScript, which doesn’t support types. If you make a mistake in a function or a variable name, it’s displayed in red. If you pass the wrong number of parameters (or wrong types) to a function, again, the errors are displayed in red. IDEs also offer great context-sensitive help. TypeScript code can be refactored by IDEs.

TypeScript follows the latest ECMAScript specifications and adds to them types, interfaces, decorators, class member variables (fields), generics, enums, the keywords public, protected, and private, and more. Check the TypeScript roadmap on GitHub at https://github.com/Microsoft/TypeScript/wiki/Roadmap to see what’s coming in future releases of TypeScript.

In the second line, specifying the type string is unnecessary. Since the variable is initialized with the string, TypeScript will guess (infer) that the type of name2 is string.

If you use types, TypeScript’s transpiler can detect mismatched types during development, and IDEs will offer code completion and refactoring support. This will increase your productivity on any decent-sized project. Even if you don’t use types in declarations, TypeScript will guess the type based on the assigned value and will still do type checking afterward. This is called type inference.

The following fragment of TypeScript code shows that you can’t assign a numeric value to a name1 variable that was meant to be a string, even though it was initially declared without a type (JavaScript syntax). After initializing this variable with a string value, the inferred typing won’t let you assign the numeric value to name1:

Testing Angular Applications

This is an excerpt from Manning's book Testing Angular Applications.

In the Cat class, notice that the keyword string is sprinkled throughout the example. This is how you specify the expected types in TypeScript. You can specify return types, variable types, and parameter types within your code. The ability to specify types—type annotations—is the key advantage of using TypeScript, hence the name.

  • The configureTestingModule method takes in an object that has to use the TestModuleMetadata interface. You can either create a variable that sets the type to TestModuleMetadata and then pass the variable into the configureTestingModule method, or create an object with the relevant configuration data and then pass that into the configureTestingModule method.