This TypeScript code covers various fundamental concepts, including variables, arrays, enums, types, interfaces, generics, classes, inheritance, and more.
message
andmsg
are declared with thestring
type.count
is declared as anumber
.isTrue
is a boolean variable.
num
is an array of numbers, demonstrating array initialization and pushing values.message2
is an array of strings, showcasing string array initialization.
score
andsrc
are examples of empty arrays.readOnlyArray
is declared as a read-only number array.
myTuple
demonstrates the usage of a tuple, combining different data types in an array.DE06
is an array of tuples representing student information.
- Two examples of enums (
Colors
andTshirts
) are provided, showcasing their usage.
x
is of theany
type, allowing dynamic typing.notSure
andmsg3
demonstrate theunknown
type, which is similar toany
but requires type assertion.
myFunc
is a function returningvoid
, meaning it doesn't return any value.- The
never
type is introduced but commented out in an error-generating example.
d
is a variable that can be of typestring
,number
, orboolean
.
- A type alias named
Num
is introduced for a union type ofstring
ornumber
. - The
checkNumber
function takes two parameters of typeNum
and returns aNum
.
pet
is a type representing a pet with string literal values "cat" or "dog".
- Two examples of interfaces (
Person2
andPerson
) showcase the structure of objects.
- The
Employee
class demonstrates a simple class constructor. - The
Person
andIsci
classes show inheritance, with the latter extending the former.
- The
Calender
interface is implemented by theLMSCalender
class. - The class contains an array of events, and the
addEvents
method adds events to it.
- The
getArray
function is a generic function that creates an array based on the provided items. - The
keyValuePair
class demonstrates a generic class with multiple types.
- The
Sum
interface defines a function signature for addition, and it is implemented by theadd
function.
This TypeScript code provides a comprehensive overview of various language features, helping developers understand and practice TypeScript concepts. Each section demonstrates a specific aspect of the language, making it suitable for learners and developers looking for a quick reference.