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

ReadonlyArray type inferr for Array.isArray issue #29600

Closed
marjune163 opened this issue Jan 26, 2019 · 1 comment
Closed

ReadonlyArray type inferr for Array.isArray issue #29600

marjune163 opened this issue Jan 26, 2019 · 1 comment
Assignees
Labels
Duplicate An existing issue was already created

Comments

@marjune163
Copy link

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:

@NN---
Copy link

NN--- commented Jan 26, 2019

#17002

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Feb 5, 2019
@RyanCavanaugh RyanCavanaugh self-assigned this Feb 5, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.4.0 milestone Feb 5, 2019
@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants