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

Interaction of empty arrays and satisfies is unsatisfying #51853

Open
5 tasks done
Hental opened this issue Dec 11, 2022 · 7 comments
Open
5 tasks done

Interaction of empty arrays and satisfies is unsatisfying #51853

Hental opened this issue Dec 11, 2022 · 7 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Hental
Copy link

Hental commented Dec 11, 2022

Suggestion

πŸ” Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

when we use satisfies keywords, infer empty array to typed array by satisfies target array instead of never array

πŸ“ƒ Motivating Example

interface TreeNode {
  children: TreeNode[];
}

const a = {
  children: [],
} satisfies TreeNode;

// expect: TreeNode[]
// accept: never[]
a.children
// ^?

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgCpQhAcgewCYoDeAUMsggBbAA2eGIAXGhtvhANoC6A3MQL7FiCHCADOYZHGQBeZCTKUadCI2RcANP2Si4YYKJjAIo5plwFexAPRXkEAB4AHCAjBN0Ztl2JwAdItp6a1sAPQB+YiA

πŸ’» Use Cases

infer a.children to TreeNode[]

@jcalz
Copy link
Contributor

jcalz commented Dec 11, 2022

The functionality of satisfies, as implemented in #46827, was discussed in #7481 and then #47920, and it was decided that the "safe upcast" functionality couldn't really be directly supported while also supporting all the other use cases. In this comment it was shown that to get a safe upcast you could write x satisfies T as T which, while annoying, is at least possible:

const a = {
  children: [],
} satisfies TreeNode as TreeNode; // safe upcast

But if you're already assigning to a variable you might as well annotate:

const a: TreeNode = { // annotate
  children: [],
};

Or, depending on what you're trying to do:

const a = {
  children: [] satisfies TreeNode[] as TreeNode[] // safe upcast here
} satisfies TreeNode;

I think it would be wonderful to have a specific operator for safe upcasting (shorten x satisfies T as T to x satisfiesas T / x satisfas T / x sassafras T / x sass T / x ass T), like an in-line type annotation. (Is there an open issue for this?) But I don't think satisfies alone will ever do this for you.

(Note: for empty arrays in particular it would lovely if nothing were ever inferred as never[], but I don't know that they'd want to special case how empty arrays behave with satisfies.)

@fatcerberus
Copy link

fatcerberus commented Dec 11, 2022

(Note: for empty arrays in particular it would lovely if nothing were ever inferred as never[], but I don't know that they'd want to special case how empty arrays behave with satisfies.)

I believe @RyanCavanaugh is on record as saying that nothing should ever be inferred as never[] but I can't find the relevant comment at the moment.

edit: Found it. #47531 (comment)

@RyanCavanaugh
Copy link
Member

It's probably time to revive #47898

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Dec 12, 2022
@RyanCavanaugh RyanCavanaugh changed the title update infer type when use satisfies keyword Interaction of empty arrays and satisfies is unsatisfying Dec 12, 2022
@RyanCavanaugh RyanCavanaugh self-assigned this Dec 12, 2022
@RyanCavanaugh
Copy link
Member

Some notes from discussion and post-discussion thinking

"Copying" the contextual type is just wrong (the first revision of the PR didn't do this either) because you might have const m = [] satisfies MyCustomArrayType, which should fail. You need to get the element type of the array and construct an array type from it.

Which element types, though? You might write

const m = [] satisfies ArrayLike<number>

where the desired type is presumably m: number[]

For unions, things get tricky. It's tempting to say that for an offered type T[] | U[], we should produce either T[] | U[] or (T | U)[], but neither of these are right. Counterexamples:

// Should pass, but would fail if [] has type (string | number)[]
const m: string[] | number[] = [] satisfies string[] | number[]
type Obj = { a: string[] } | { a: number[] };
// Should pass, but would fail if [] has type string[] | number[]
const m: Obj = { a: [] } satisfies Obj;

These examples are illustrative -- the only thing that works here is to give the [] type never[] (this is what it has "in reality" today), where never is specifically derived from (T & U)[]

I'll update the PR to implement the behavior that we give an empty array a type of the intersection of its contextual types' element types if they are ArrayLike

@fatcerberus
Copy link

fatcerberus commented Dec 13, 2022

Why is the default inference for [] (in the absence of a contextual type) not unknown[]? It feels weird to infer never[] since anyone initializing a variable with an empty array (or returning one from a function without a return type annotation) likely intends to push stuff into it afterwards. Is it just to encourage writing explicit type annotations in this case?

@RyanCavanaugh
Copy link
Member

Why is the default inference for [] (in the absence of a conditional type) not unknown[]? It feels weird to infer never[] ...

Good question and a lot to unpack here. I'll speak only in terms of today's behavior for clarity. Empty arrays are (again, currently) not affected by contextual typing except that they can form 0-length tuples in the presence of a tuple-like contextual type. So if you look at a call like

declare function doSomething(blah: number[]): void
doSomething([]);

Here the expression [] has type never[] and the call succeeds. Had [] had type unknown[], the call would have failed.

For types given to inferred variables, the situation is much tricker due to evolving arrays. But object properties don't qualify for evolving arrays, so the situation is analogous:

const obj = { a: [] }
declare function doSomething(blah: { a: readonly number[] }): void
doSomething(obj);

This call is processed the same way: obj.a is never[]. This one is probably the most important to get right since there is no contextual typing to "fix" the type of [] in this example.

likely intends to push stuff into it afterwards

Sometimes yes, sometimes no. There are a lot of APIs like

sendEmail(subject, body, /*attachments*/ []);

where it makes sense to just give someone a fresh empty array literal that doesn't have any bindings pointing to it. The "sometimes yes" case is indeed why we went to the trouble to make evolving arrays (those whose type is inferred via CFA looking for calls to push/etc), since in the case where you start with a variable initialized with [], almost certainly it's not going to stay empty.

TL;DR handling both covariant and contravariant inference is hard

@msand
Copy link

msand commented May 9, 2023

@RyanCavanaugh I hit upon this issue yesterday in a case where I want to access an array property of an object where only some of the possible properties are defined:

type Example = {
    a?: string[] | null;
    aa?: string[] | null;
}

const example = {
    a: [],
} satisfies Example;

const { a } = example;

// Argument of type 'string' is not assignable to parameter of type 'never'.(2345)
a.push('');

type NonNullableFields<T> = {
    [P in keyof T]: NonNullable<T[P]>;
};

const workaround: NonNullableFields<Required<Example>> = {
    a: [],
    aa: [],
}

const { a: b } = workaround;

b.push('');

https://www.typescriptlang.org/play?#code/C4TwDgpgBAogHgQwLZgDbQLxQN4CgoFQID8AXFAM7ABOAlgHYDmA2gLpQA+U9ArqqgG58hBCXJU6TNp259BuAL65cAYwD29KlAiIU6KFjyEi5NgBpFlBMFoUAZrQgVYutBCGqNW7ESgKD2q7oHggAdGA8FAAWABQA5HEAlB6gkFAAchrpcggARugAYo6oACYUADwAKgB8AUaEzAAKUAxQANYQIGp2UJWs5Jn02fx56FVNrNVCCh7qmsBQAO5q1G0I1Go89CUDWTn5EEUQpRUAShAAjjy01BAl5fDIbtW1hsIECKasFsaiXxZKTzzHAmKC5PwBZardabbYeXLhSKxBLJIA

The safe upcast pattern doesn't work here, only workaround I found was to define all the properties and make a NonNullableFields<Required<Example>> annotation on the variable, this requires unnecessary arrays to be defined. Is there any better workarounds for this pattern?

@RyanCavanaugh RyanCavanaugh removed their assignment Aug 2, 2024
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed In Discussion Not yet reached consensus labels Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants