Skip to content

string is incompatible with string literal #6789

@youngzhao-xyz

Description

@youngzhao-xyz

I got flow error from this code snippet

type MyType = 'aa' | 'bb'
    
const str = 'a'
const test1: MyType = `a${str}`	// ERROR
const test2: MyType = 'a' + str	// ERROR

Error

    const test1: MyType = `a${str}`	// ERROR
                             ^ Cannot assign template string to `test1` because: Either string [1] is incompatible with string literal `aa` [2]. Or string [1] is incompatible with string literal `bb` [3].
    References:
    6: const test1: MyType = `a${str}`	// ERROR
                             ^ [1]
    3: type MyType = 'aa' | 'bb'
                     ^ [2]
    3: type MyType = 'aa' | 'bb'                        ^ [3]

Link: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCyWAKrgQLxgDkAhrdWAD40BGr16AxnAHYDOGMIIBOYKnU48BQjHkEBGAFxFS5cWAAGtACQBvUQF9NASGDAwAUQBK1gPLXU0wWDmCATCuJl8GyWABqYQwRMwsbe2sgA

Does anyone know why flow doesn't support this? Or is there a better way to write this code? Thanks!

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