1

Assume I have such code:

function concat(a: string, b: string):string {
    return a + b;
}

function firstPlusWorld(items: string[]):string {
    return concat(items[0], ' World');
}

console.log(firstPlusWorld([]));

In this case typescript won't argue that the first argument passed to concat is not a string.

In functional land there is Maybe type and head function has type signature

head :: List a -> Maybe a

Is there any way to make typescript handle such cases?

1
  • Non-nullable types are a very good concept, but they're only integrated in a limited number of languages thus far. JavaScript, and its derivative project TypeScript, are not among them. Commented Dec 18, 2015 at 18:36

2 Answers 2

1

Unfortunately, you will have to resort to good/old if tests. Typescript does not add anything to javascript in that respect.

There are tentatives to bring Monads to Javascript (and Typescript by extension) such as this one: https://github.com/fantasyland/fantasy-land (https://github.com/fantasyland for implementations)

I am still frustrated at this stage and if anyone finds a good library, I will be glad to jump on it and write typescript definition files for it if need be.

On the data structures and immutability front, things are a bit better with Immutable.js but it would be great to wrap the structures in Monads so operations on them could be better chained.

Sign up to request clarification or add additional context in comments.

Comments

0

Actually, string as the TypeScript type can also accept null:

function concat(a: string, b: string):string {
    return a + b;
}

concat(null, null);

Does this answer your question?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.