Skip to content

add a flag to disallow expression statements different than void #8615

Closed
@zpdDG4gta8XKpMCd

Description

@zpdDG4gta8XKpMCd

Problem:
Funtional programming needs better support in TypeScript. Values are everything in FP and in TypeScript the expression statements just silently discard them. Not being able to track places where the values are discarded leads to severe bugs. Example:

// before refactoring
function notify(message: string) : void {
    window.postMessage(message); // <-- assuming always works
}

// turns out that window.postMessage crashes when window gets closed
// need a check and a conditional result

// after refactoring
function notify(message: string) : Tried<void, 'cant-send-a-message'> {
    if (window.closed) {
        return failureFrom('cant-send-a-message');
    } else {
        return successFrom(window.postMessage(message));
    }
}

// after such change we need to make sure that all places that call `notify` are addressed
// it's easy to overlook those without a help from a compiler

Workaround:
use a linter rule

Solution:
add a flag to the compiler

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions