Skip to content

Type [x, x] is not assignable to type x[] & { length: 2; } #44508

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

Open
strogonoff opened this issue Jun 8, 2021 · 2 comments
Open

Type [x, x] is not assignable to type x[] & { length: 2; } #44508

strogonoff opened this issue Jun 8, 2021 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@strogonoff
Copy link

strogonoff commented Jun 8, 2021

Bug Report

This error is probably self-explanatory. Either this is supposed to work and it’s a bug, or I’m misunderstanding something in which case please feel free to close.

Type '(Record<string, any> | null)[] & { length: 2; }' is not assignable to type '[Record<string, any> | null, Record<string, any> | null]'.ts(2322)

Outer function is typed to return the latter. It calls an inner function, typed to return the former. One would assume this should work.

Possibly related: #44309

🔎 Search Terms

array literal length

🕗 Version & Regression Information

TypeScript 4.1.3

💻 Code

async function readObjectVersions
<L extends number, C extends string[] & { length: L }>
(objectPath: string, versions: C):
Promise<(Record<string, any> | null)[] & { length: L }> {
  return versions.map(version => ({ foo: 'bar' }));
}

const version1: string = 'x';
const version2: string = 'y';

async function _readObjectVersions(objectPath: string):
Promise<[ Record<string, any> | null, Record<string, any> | null ]> {
  return await readObjectVersions(objectPath, [version1, version2]);
}

_readObjectVersions('abc');

🙁 Actual behavior

The error as described above.

🙂 Expected behavior

No type error.

@strogonoff strogonoff changed the title [x, x] is not assignable to type x[] & { length: 2; } Type [x, x] is not assignable to type x[] & { length: 2; } Jun 8, 2021
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug In Discussion Not yet reached consensus Suggestion An idea for TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Jun 8, 2021
@RyanCavanaugh
Copy link
Member

We'd need to add a special rule to detect this, since there's currently no logic in place to reason about whether the 0 and 1 properties are present or not.

@strogonoff
Copy link
Author

I suppose such a special rule could rely on the length property—if it can be assumed to always exist on an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants