0% found this document useful (0 votes)
5 views18 pages

Typescript Annotations Assertions Inference

The document discusses TypeScript's features including annotations, inferences, and assertions. It explains how type annotations enforce type checking, how TypeScript infers types when not explicitly defined, and how type assertions allow programmers to specify a variable's type. The document also contrasts annotations, inferences, and assertions with examples for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Typescript Annotations Assertions Inference

The document discusses TypeScript's features including annotations, inferences, and assertions. It explains how type annotations enforce type checking, how TypeScript infers types when not explicitly defined, and how type assertions allow programmers to specify a variable's type. The document also contrasts annotations, inferences, and assertions with examples for clarity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CYPRESS SESSION DAY-4

BY
VIGNESH GOPIRAJ
RENGANATHAN
TODAYS TOPIC:

Today we •ANNOTATIONS,
going to •INFERENCES, and
discuss •
about ASSERTIONS.
Annotations

TypeScript - Type
The following example declares variables with
Annotations different data types:

• TypeScript is a typed language, where


we can specify the type of the
variables, function parameters and
object properties. We can specify the
type using :Type after the name of the
variable, parameter or property. There
can be a space after the colon.
TypeScript includes all the primitive
types of JavaScript- number, string and
Boolean.
• In the above example, each variable is declared with their data
type. These are type annotations. You cannot change the value
using a different data type other than the declared data type of a
variable. If you try to do so, TypeScript compiler will show an
error. This helps in catching JavaScript errors. For example, if you
assign a string to a variable age or a number to name in the
above example, then it will give an error.

• Type annotations are used to enforce type checking. It is not


mandatory in TypeScript to use type annotations. However, type
annotations help the compiler in checking types and helps avoid
errors dealing with data types. It is also a good way of writing
code for easier readability and maintenance by future developers
working on your code.
• We can still follow
the JavaScript way The following example demonstrates the
type annotation of parameters.
of declaring
variables and have
the TypeScript
compiler infer the
data type of the
variable.
• Similarly, we can declare an
object with inline annotations
for each of the properties of
the object.
• Here, we declare an object employee
with two properties id and name with
the data type number and string
respectively.
• If you try to assign a string value to id
then the TypeScript compiler will give
the following error.
Type Inference in TypeScript
• TypeScript is a typed language.
However, it is not mandatory to specify
the type of a variable. TypeScript infers
types of variables when there is no
explicit information available in the form
of type annotations.
Inference • Types are inferred by TypeScript
compiler when:
• Variables are initialized
• Default values are set for parameters
• Function return types are determined
• For example,
var a = "some text“
• Here, since we are not explicitly defining a: string with a type
annotation, TypeScript infers the type of the variable based on
the value assigned to the variable. The value of a is a string and
hence the type of a is inferred as string.
• Consider the following example:

• The above code shows an error because while inferring types,


TypeScript inferred the type of variable a as string and variable b
as number. When we try to assign b to a, the compiler complains
saying a number type cannot be assigned to a string type.
• There may be scenarios where an object may be initialized with
multiple types.
• For example:
• var arr = [ 10, null, 30, 40 ];

Type • In the above example, we have an array that has the values 10,
inference in null, 30, and, 40 . TypeScript looks for the most common type to
infer the type of the object. In this case, it picks the one thats is

complex compatible with all types i.e. number, as well as null.


• Consider another example:
objects • var arr = [0, 1, "test"];

• Here, the array has values of type number as well as type string. In
such cases, the TypeScript compiler looks for the most common
type to infer the type of the object but does not find any super
type that can encompass all the types present in the array. In such
cases, the compiler treats the type as a union of all types present in
the array. Here, the type would be (string | number) which means
that the array can hold either string values or number values. This
is called union type.
• Lets try to add a new element to the array:

• The compiler accepts the new value since the new value is of type
string which is okay.
• Now, lets try to add a new type to the array which was not already a
part of the array:
• The above code will show a compiler error because boolean is not a part
of union (string | number).

• The return type of a function is also inferred by the returning value. For
example:

• In the above function, return type of the function sum is number. So, the
result can be stored in a number type variable but not a string type
variable.

• Thus, type inference is helpful in type-checking when there are no explicit


type annotations available.
What is Assertion?
Type assertion is a technique that informs the compiler about the type of a variable.Typescript compiler
automatically determines the data type of any value or variable. Using Type assertion, we can specify a
value’s type and tell the compiler not to deduce it, as Programmers have a provision to set a data type of
their own.

When we want to change a variable from one type to another such as any to number, String, or
Boolean , we use Type assertion.

So Type assertion is explicitly telling the compiler that we want to treat the entity as a different type. It
allows us to treat any as a number, or number as a string. Type assertion is widely used when we are
migrating codes from JavaScript to TypeScript.
• Typescript Assertions can

Way of
be written using

1. Angular Brackets <>

using Type
2. “as” Keyword

any/unknown must be

Assertions:
specified before using Type
assertions.
Type
assertion
using:
<angular
bracket>
Type Assertion Using “as” Keyword
Example for
Boolean data
type
Difference between the Annotation, Inference and Assertion:

S.NO ANNOTATIONS INFERENCES ASSERTIONS

1.Definition Type Type inference is Type Assertions is a


annotation involves a Typescript compiler mechanism that
standard way of automatically provides programmers
explicitly specifying the determines the a feasibility of setting
datatype using the datatype depends of up their own data type,
colon symbol followed the input value. without the
by the type of variable interference of
we want to use. typescript compiler.
2. Example let a: number = 500; let a = 500; let a : any = 500;
let b = <number>a
let b = <string>a
THANK YOU ALL

You might also like