Skip to content

Suggestion: Enforce declaring method return types #333

@xirzec

Description

@xirzec

Applicable ESLint rules:

Basically, consider a decorator helper like:

export function getStatusCodes(program: Program, entity: Type) {
  return program.stateMap(statusCodeKey).get(entity);
}

The inferred type is any because that's what stateMap returns, but really we know for this specific case it's string[] | undefined (or string[] when you have done an isStatusCode() check first.)

This requires consumers to look into our actual implementation code to determine the data types returned from decorator state methods, which is a bit time consuming and annoying. It also means that breaks could easily occur as we're not enforcing a public contract but relying on casting from any.

Consider the alternative:

export function getStatusCodes(program: Program, entity: Type): string[] {
  return program.stateMap(statusCodeKey).get(entity) ?? [];
}

Now a consumer knows exactly what they're going to get and they never have to worry about the undefined case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions