Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array.isArray() type guard doesn't recognize ReadonlyArray<T> #19892

Closed
jrockwood opened this issue Nov 10, 2017 · 2 comments
Closed

Array.isArray() type guard doesn't recognize ReadonlyArray<T> #19892

jrockwood opened this issue Nov 10, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jrockwood
Copy link

TypeScript Version: 2.6.1

Code

interface StringRecord {
    readonly message: string | ReadonlyArray<string>; 
}

const record: StringRecord = { message: 'item' };
const message: string = Array.isArray(record.message) ? record.message.join('') : record.message;

Expected behavior:
The Array.isArray() type guard check should detect that record.message is a string. Note that if the message field is changed to string | string[] then everything works as expected. It's only when it's a ReadonlyArray<T> that it fails to compile. I can definitely add a as string cast, but that's kind of ugly and potentially dangerous if I change the data type later.

Actual behavior:
An error is reported: error TS2322: Type 'string | ReadonlyArray<string>' is not assignable to type 'string'.

@ghost
Copy link

ghost commented Nov 10, 2017

Looks like a duplicate of #17002.

@ghost ghost added the Duplicate An existing issue was already created label Nov 10, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants