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

Regression - default initializer of {} ruins type intersected with object #49480

Closed
DanielRosenwasser opened this issue Jun 10, 2022 · 13 comments · Fixed by #49503
Closed

Regression - default initializer of {} ruins type intersected with object #49480

DanielRosenwasser opened this issue Jun 10, 2022 · 13 comments · Fixed by #49503
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros

Comments

@DanielRosenwasser
Copy link
Member

export interface BarProps {
    barProp?: string;
}

export interface FooProps {
    fooProps?: BarProps & object;
}

declare const foo: FooProps;
const { fooProps = {} } = foo;

fooProps.barProp;
//       ~~~~~~~
// error! Property 'barProp' does not exist on type '{}'.
@DanielRosenwasser
Copy link
Member Author

@typescript-bot bisect good v4.7.3 bad main

@DanielRosenwasser
Copy link
Member Author

Forgot to add strictNullChecks, but it shouldn't matter.

@typescript-bot
Copy link
Collaborator

The change between v4.7.3 and main occurred at 5aa0053.

@andrewbranch andrewbranch added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Jun 10, 2022
@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 10, 2022

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @DanielRosenwasser

‼️ Exception: Error - Did not find a source file for /home/runner/work/TypeScript/TypeScript/index.ts

Error: Did not find a source file for /home/runner/work/TypeScript/TypeScript/index.ts
    at Object.updateFile (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:6876:15)
    at _loop3 (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:1738:9)
    at twoslasher (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:1744:17)
    at /home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:777:44
    at runTwoslashRequests (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:744:56)
    at run (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:2169:75)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Historical Information
Version Reproduction Outputs Time
4.3.2, 4.4.2, 4.5.2, 4.6.2, 4.7.2

👍 Compiled

⚠️ Way slower

@fatcerberus
Copy link

The change between v4.7.3 and main occurred at 5aa0053.

That’s the second time it’s done that…

@weswigham
Copy link
Member

methinks the twoslasher is broken with latest TS - the bug workbench does the same thing.

@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Jun 10, 2022

@ahejlsberg
Copy link
Member

ahejlsberg commented Jun 12, 2022

Yup, this is caused by #49119. It modified simpleTypeRelatedTo to consider {} a supertype of object in the strictSubtypeRelation. But really that should only be true for a non-fresh {}. In other words, we want fresh {} <: object <: {} in the strict subtype relation (i.e. fresh {} is a subtype of object which is a subtype of {}).

Also, for some reason we remove object literal freshness from the source side when relating to an intersection on the target side. Lost in history as to why, removing it doesn't change any baselines.

In combination, those two changes fix the issue. I will put up a PR.

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jun 12, 2022
@DanielRosenwasser
Copy link
Member Author

In other words, we want {} <: object <: fresh {} in the strict subtype relation.

You mean the reverse, right?

fresh {}object{}

@ahejlsberg
Copy link
Member

@DanielRosenwasser Argh, yes. I always read <: as is-supertype-of because it looks like an inheritance tree on its side. But you're right, it should be the other way around. I'll correct it.

@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Jun 13, 2022

GitHub now lets you do Mermaid diagrams. This should make it easier to read 😄

graph BT;
    emptyObject("{}");
    nonPrimitive("object");
    freshEmptyObject("fresh {}");
    freshEmptyObject --> nonPrimitive --> emptyObject;
Loading

@fatcerberus
Copy link

Mermaid diagrams

I've never heard this term before.

Also, on @ahejlsberg's note, I've personally always felt like the arrows in inheritance diagrams go in the wrong direction (cause <- effect is weird), so I find fresh {} ≤ object ≤ {} to actually be easier to read.

@RyanCavanaugh
Copy link
Member

It's super weird because I want class arrows to go one way but subtyping arrows to go the other way. Human brains are a mess.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants