Type Script Type Test Part 1
Action @ StackBlitz refereed from TypeScriptLang
Base Types
Description
Any
The super set of all data types
Built-in types
Includes number, string, boolean, void, null,never, and undefined
User-defined types
Includes Arrays, Enums, Classes, Interfaces etc…,
output of the code :
Any
Base Types
|
Description
|
Any
|
The super set of all data types
|
Built-in types
|
Includes number, string, boolean, void, null,never, and undefined
|
User-defined types
|
Includes Arrays, Enums, Classes, Interfaces etc…,
|
Any Data type is a super set of all data types , it denotes dynamic type ,Defualt value : undefined
Declaration :
anydatatype: any;
Built-in types
number
Represents integer and floating type, Defualt value : undefined
Declaration :
numberdatatype: number;
string
Represents integer and floating type, Defualt value : undefined
Declaration :
stringdatatype: number;
boolean
Represents integer and boolean type true or false, Defualt value : undefined
Declaration :
booeandatatype: number;
null
Its is used when the object does not have any values, value : null
to check null :
if (this.anydatatype === null) {
console.log('Any data type value is null');
}
undefined
Something hasn't been initialized, value : undefined
To check undefined :
if (this.anydatatype === undefined) {
console.log('Any data type value undefined');
}
void
Return type of functions that do not return a value, value : void
To declare :
public welcometovoid(): void {
console.log('welcometovoid function called');
}
never
never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns, value : never
To declare :
//A function returning 'never' cannot have a reachable end point, below function wont run because it reach's the end point public welcometovoid(): never {
console.log('never end point reached');
}
// Function returning never must have unreachable end point
public errornever(): never {
throw new Error ('cant reach the end point');
}
// Function returning never must have unreachable end point
infiniteLoopnever(): never {
while (true) {
}
Assign Values
// Assign values for declared variables and console it
assignvalues(): void {
this.anydatatype = new Date();
this.booeandatatype = true;
this.numberdatatype = 50;
this.stringdatatype = 'welcome to type script data types';
console.log('anydatatype', [this.anydatatype]);
console.log('numberdatatype', [this.numberdatatype]);
console.log('stringdatatype', [this.stringdatatype]);
console.log('booeandatatype', [this.booeandatatype]);
}
Assign Null Values
// Assign null values for declared variables and console it
assignnullvalues(): void {
this.anydatatype = null;
this.booeandatatype = null;
this.numberdatatype = null;
this.stringdatatype = null;
console.log('anydatatype', [this.anydatatype]);
console.log('numberdatatype', [this.numberdatatype]);
console.log('stringdatatype', [this.stringdatatype]);
console.log('booeandatatype', [this.booeandatatype]);
}
1 Comments
useful article...
ReplyDelete