The answer to this question is both Yes and No. Similar to many other popular programming languages like Java, Typescript is also trying to combine best of both worlds for its users. Typescript is a multi-paradigm programming language and it is has influences from Object Oriented Programming as well as fucntional programming paradigm.
One of the most important concept of functional paradigm is that the languages following this paradigm ensure that the applications builts using their type system are free of side-effects. Since Typescript interoperate with Javascript, it has lost some of its freedom and unlike pure functional languages like Haskell it cannot guarantee that our applications will be free from side-effects. Languages like Haskell ensure this through their type system. We can however use FP techniques to improve our type safety. For example look at the following code snippet
You can see that the second call to findNumber will not return a number instead it will throw an error. That means the function canot guarantee a specific type of output in all situations. This is againt the functional programming paradigm.
In Functional programming languages like Haskel the default behavior of type system ensures that this will not happen. In Typescript it is up to the programmer to use an approach like the above or choose a more functional way through promises. A promise based approach will look like the following
The answer of our original question is Typescript is both functional and object oriented and it allows programmers to choose the implementation based on what they find easy for their problems.
One of the most important concept of functional paradigm is that the languages following this paradigm ensure that the applications builts using their type system are free of side-effects. Since Typescript interoperate with Javascript, it has lost some of its freedom and unlike pure functional languages like Haskell it cannot guarantee that our applications will be free from side-effects. Languages like Haskell ensure this through their type system. We can however use FP techniques to improve our type safety. For example look at the following code snippet
findNumber (numbers: Number[], input: number): number {
if(numbers.length == 0) {
throw new Error ("There are no number");
}
const number = numbers.find ( (num, index ) => {
if(input === num ) return index
});
if( number) return number;
throw new Error("Number not found!");
}
const numbers = [2, 3, 5, 21, 45, 56, 23, 2, 456]; const numberIndex = findNumber(numbers, "3"); console.log(`Number found at index ${numberIndex}` ); const numberIndex2 = findNumber([], "3")console.log(`Number found at index ${numberIndex2}` );
You can see that the second call to findNumber will not return a number instead it will throw an error. That means the function canot guarantee a specific type of output in all situations. This is againt the functional programming paradigm.
In Functional programming languages like Haskel the default behavior of type system ensures that this will not happen. In Typescript it is up to the programmer to use an approach like the above or choose a more functional way through promises. A promise based approach will look like the following
findNumber (numbers: Number[], input: number) : Promise<number> {
if(numbers.length == 0) {
return Promise.reject(new Error("There are no numbers") );
}
const number = numbers.find ( (num, index ) => {
if(input === num ) return Promise.resolve(index)
});
if( number) return number;
return Promise.reject(new Error("Number not found") );
}
findNumber(numbers, "3").then ( idx => console.log(`Number found at index ${idex}` )
The above example shows how we can transform our unsage and impure function into a safe and pure function.The answer of our original question is Typescript is both functional and object oriented and it allows programmers to choose the implementation based on what they find easy for their problems.
Comments
Post a Comment