Skip to content

mf2 MessagePart type can't be meaningfully discriminated on type property #444

@lionel-rowe

Description

@lionel-rowe

MessagePart, the element type of MessageFormat#formatToParts's return type, expands to MessageBiDiIsolationPart | MessageExpressionPart | MessageLiteralPart | MessageMarkupPart, of which MessageExpressionPart's type property is simply string. As a result, the MessageParts have no ergonomic way of discriminating between the different types of message parts:

for (const part of new MessageFormat('en', 'hello world').formatToParts()) {
    part.type
    //   ^ type: `string` (should be `'literal' | 'markup' | 'bidiIsolation' | ...other string literals`)
    
    switch (part.type) {
        case 'literal': {
            part
            // ^ type: `MessageLiteralPart | MessageExpressionPart` (should be `MessageLiteralPart` only)
            break
        }
        case 'markup': {
            part
            // ^ type: `MessageMarkupPart | MessageExpressionPart` (should be `MessageMarkupPart` only)
            break
        }
        case 'bidiIsolation': {
            part
            // ^ type: `MessageBiDiIsolationPart | MessageExpressionPart` (should be `MessageBiDiIsolationPart` only)
            break
        }
        default: {
            part
            // ^ type: generic `MessagePart` (should be `MessageExpressionPart`, or better yet a union of finer-grained types)
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions