Skip to content

ReadonlyArray type inferr for Array.isArray issue #29600

@marjune163

Description

@marjune163

Search Terms:
ReadonlyArray, inferr

Code

interface User {
	name: string;
	age: number;
};
type Param = ReadonlyArray<User> | User;
// change to this will work:
// type Param = Array<User> | User;

function printObject(param: Param) {
	if (Array.isArray(param)) {
		console.log("array", param.length);
	}
	else {
		/*
		Property 'age' does not exist on type 'Param'.
			Property 'age' does not exist on type 'ReadonlyArray<User>'. [2339]
		*/
		console.log("object", param.age);
	}
}

Expected behavior:

$ tsc

Actual behavior:

$ tsc
index.ts(18,31): error TS2339: Property 'age' does not exist on type 'Param'.
  Property 'age' does not exist on type 'ReadonlyArray<User>'.

--

Playground Link:
http://www.typescriptlang.org/play/index.html#src=interface%20User%20%7B%0D%0A%09name%3A%20string%3B%0D%0A%09age%3A%20number%3B%0D%0A%7D%3B%0D%0Atype%20Param%20%3D%20ReadonlyArray%3CUser%3E%20%7C%20User%3B%0D%0A%2F%2F%20change%20to%20this%20will%20work%3A%0D%0A%2F%2F%20type%20Param%20%3D%20Array%3CUser%3E%20%7C%20User%3B%0D%0A%0D%0Afunction%20printObject(param%3A%20Param)%20%7B%0D%0A%09if%20(Array.isArray(param))%20%7B%0D%0A%09%09console.log(%22array%22%2C%20param.length)%3B%0D%0A%09%7D%0D%0A%09else%20%7B%0D%0A%09%09%2F*%0D%0A%09%09Property%20'age'%20does%20not%20exist%20on%20type%20'Param'.%0D%0A%09%09%09Property%20'age'%20does%20not%20exist%20on%20type%20'ReadonlyArray%3CUser%3E'.%20%5B2339%5D%0D%0A%09%09*%2F%0D%0A%09%09console.log(%22object%22%2C%20param.age)%3B%0D%0A%09%7D%0D%0A%7D

Related Issues:

Metadata

Metadata

Assignees

Labels

DuplicateAn existing issue was already created

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions