Created
March 25, 2026 16:49
-
-
Save 5cover/5b637893daec0bb2c78e96e8b419860f to your computer and use it in GitHub Desktop.
TypeScript unsoundness demonstration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Box<T> = { t: string, value: T } | |
| type A =1; | |
| type B= {a:0} | |
| type C= 'hi' | |
| const boxes1 = [ | |
| { t: 'number', value: 1 satisfies A } , | |
| { t: 'object',value: {a:0} satisfies B }, | |
| { t: 'string',value: 'hi' satisfies C }, | |
| ] as const satisfies Box<any>[]; | |
| const boxes2 = [ | |
| { t: 'object',value: {a:0} satisfies B }, | |
| { t: 'number',value: 1 satisfies A } , | |
| { t: 'string',value: 'hi' satisfies C }, | |
| ] as const satisfies Box<any>[]; | |
| checkCoherence(1) | |
| for (let i = 0; i < boxes1.length; ++i) { | |
| const box1 = boxes1[i]; | |
| const box2 = boxes2[i]; | |
| f<symbol>(box1, box2); | |
| } | |
| checkCoherence(2) | |
| function f<T>(box1: Box<T>, box2: Box<T>) { | |
| box1.value = box2.value; | |
| } | |
| function checkCoherence(n: number) { | |
| for (let i = 0; i < boxes1.length; ++i) { | |
| if (typeof boxes1[i].value !== boxes1[i].t) console.error(n, 'inconsistent boxes1', i, 'expected', boxes1[i].t, 'got', typeof boxes1[i].value); | |
| if (typeof boxes2[i].value !== boxes2[i].t) console.error(n, 'inconsistent boxes2', i, 'expected', boxes2[i].t, 'got', typeof boxes2[i].value); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment