Skip to content

Mark/infer a function or property as pure/stateless #3882

Closed
@dead-claudia

Description

@dead-claudia

I think a way to mark and/or infer a function as having no state would be extremely beneficial for code optimization. If a function incurs no state, there are a variety of optimizations that can be applied to it that otherwise couldn't be done. Also, getters and functions that don't modify state can be removed if their results aren't used. Class methods could do the same thing.

Another area where this could be helpful is that if it's explicitly marked, it would be compile-time checked to be fully immutable, i.e. it cannot indirectly modify state. The checks to infer/assert can be independent of let/const, which would make things easier.

An explicit marker would also be very useful for type definition files, given some libraries like Lodash and React mostly consist of mutable methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions