Skip to content

Feature Request: Use non-optional properties in types #29

@qwrwed

Description

@qwrwed

Is your feature request related to a problem? Please describe.
I've been using the types from this module - they've been very useful, so thank you. Lately I've been having to work around the optional properties (example). TypeScript gives me errors such as:

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.

Object is possibly 'undefined'.

Type 'undefined' cannot be used as an index type.

This is because every property (that I've seen) is optional, which means that something which should be type e.g. string is actually inferred to be type string | undefined, requiring use of type casts (modeName as string), non-null assertions, and/or optional chaining operators (stopPointsOnLines?.[modeName!]?.[line]).

Do they need to be optional? In my experience, the TFL API always returns something for each field, even if that's just an empty value (e.g. empty list), and if some field is missing, we might not want to just continue as though there were no issues. There may be some optional fields in TFL's spec, but I feel as though these would be the exception, rather than the rule.

Describe the solution you'd like
I propose making most, if not all, of the properties non-optional, so e.g. TfL['Mode'] would go from:

Mode: {
        isTflService?: boolean;
        isFarePaying?: boolean;
        isScheduledService?: boolean;
        modeName?: string;
    };

to

Mode: {
        isTflService: boolean;
        isFarePaying: boolean;
        isScheduledService: boolean;
        modeName: string;
    };

This would mean that instead of e.g. (property) isTflService?: boolean | undefined, we'd just have (property) isTflService: boolean, instead of needing to implement some way of telling TS it shouldn't be undefined for each property.

Describe alternatives you've considered
Workarounds as above, but this feels somewhat hacky/clunky.

Additional context
This information is based on my experience of using VSCode and create-react-app on Windows.

image

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions