You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a known unsoundness, intentionally allowed. Approximately nobody wants the following code to produce an error:
functionfoo(arg: {a: number,b?: string}){console.log(arg.a.toFixed(),arg.b?.toUpperCase())}constx={a: 123};foo(x);// <-- ERROR?! We don't know that x is lacking the b property
See #42479 (assignment transitivity is broken), #13043 (for parameters but it's the same issue), #40529 (for index signatures but it's the same issue), #12936 (the exact types feature that would be needed to possibly improve this situation, so typeof x would know that a is the only property).
✅ This wouldn't be a breaking change in existing TypeScript/JavaScript code
This would most certainly break a bunch of existing TypeScript code; soundness improvements almost always do.
🔍 Search Terms
Introduction:
Consider the following TypeScript types:
Is the compatibility between types Ob1 and Ob2 part of an intended trade-off within TypeScript's type system?
https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true#code/C4TwDgpgBA8gRgRigXigbwFBSgQwFxQDOwATgJYB2A5gDRZRwD8BFEAbhCRgL4YaiRYcAEwp09fEVKVaPPgOjwAzGMzZJxctTrYmLAK4BbOJzn9wiuABZVEgppk6GB46d7nB8AKy3196dr0elD6FAAmEABmlBBhZgpCAGy+uP5asrrMIRQA1hQA9gDuFGYRAMYANjgk0BUQwFAUCATwCBjlVTVQdQ0Uwi0i7RCV1bX1jUoDSkMjXT2NVgNWM51jvV4DXiuj3eMUiQOJfAD0x7AA0hjzZTiEEM1CSKh9V+M3dwgA+g-wos9tGFOUAAoiQSPkuNdbhB+o8xBRplC7pM4c9lkDQeCSFAyJEoBAAB44MrAGBgYBkfIUHAVAAK4MgJFAABULIRkKR9BBXg13hBFqjGlskRANoL9kA
Or,
✅ Viability Checklist
⭐ Suggestion
Introduce stricter type checking for optional never to prevent runtime errors, such as:
📃 Motivating Example
The inconsistency above can lead to unintended runtime errors. For instance:
https://www.typescriptlang.org/play/?#code/C4TwDgpgBACghgJzgWwM5QLxQN5TgfgC4oA7CANwgQBooAjY1YBASxIHMoBfKAHxzyNmbdrQDGQ1h24AoGWID2JJlABmAVxJiAgpigAKMIhSpi8JGgCUkkZgB8OGVCgtVBoxdQA6OFACEGFiaACYQqmwQwZaOzs4IEMDqCCRQHiZeYk7cUBAANqjQ2FlxCUkpaWhedFlcMrXySioaWgBCeobGaMS4DFBMUpxc0RgORc6KysBQ8ajquVNYzTodnpZZE6gKuRBeuQrs+gBEAEoQs-NQADIQHMAAFsSHtDNzwLs37PfRAPTf05rAFjIaBUBAKBB1OQbKYVdBYXBwYgAcjgSNovSRdCRsiWLRWJksQA
The code compiles without errors but causes runtime issues.
💻 Use Cases
The text was updated successfully, but these errors were encountered: