Title: Moving an unrelated `new Schema
Open Graph Title: Moving an unrelated `new Schema
X Title: Moving an unrelated `new Schema
Description: 🔎 Search Terms check order, declaration order, statement order, conditional type infer never, mongoose Model, history dependent diagnostics, checkers 🕗 Version & Regression Information Reproduces identically with 5.9.3, 6.0.2, 7.0.2 and ...
Open Graph Description: 🔎 Search Terms check order, declaration order, statement order, conditional type infer never, mongoose Model, history dependent diagnostics, checkers 🕗 Version & Regression Information Reproduces i...
X Description: 🔎 Search Terms check order, declaration order, statement order, conditional type infer never, mongoose Model, history dependent diagnostics, checkers 🕗 Version & Regression Information Reproduc...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/65607
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Moving an unrelated `new Schema\u003cT>(…, options)` above a mongoose model turns `typeof Model extends Model\u003cinfer R>` into `never`","articleBody":"### 🔎 Search Terms\n\ncheck order, declaration order, statement order, conditional type infer never, mongoose Model, history dependent diagnostics, checkers\n\n### 🕗 Version & Regression Information\n\nReproduces identically with 5.9.3, 6.0.2, 7.0.2 and 7.1.0-dev.20261118.1, so it is not a regression of the native port.\n\n### ⏯ Playground Link\n\nNeeds `mongoose@9.11.2` and `@types/node`, so no playground link. Each of the two files below is a complete program on its own.\n\n### 💻 Code\n\n`first.ts`: the unrelated `historySchema` declared before the model.\n\n```ts\nimport mongoose, { type Model, Schema } from \"mongoose\";\n\nexport const historySchema = new Schema\u003c{ updatedAt: Date }>({ updatedAt: Date }, { _id: false });\n\nconst sentFileSchema = new Schema({\n agency: { type: Schema.Types.ObjectId, required: true },\n});\n\nexport const SentFileModel = mongoose.model(\"SentFile\", sentFileSchema);\n\ntype SentFile = typeof SentFileModel extends Model\u003cinfer Raw> ? Raw : never;\n\nexport const readAgency = (file: SentFile) => file.agency;\n```\n\n`last.ts`: the same statements, with `historySchema` moved to the end.\n\n```ts\nimport mongoose, { type Model, Schema } from \"mongoose\";\n\nconst sentFileSchema = new Schema({\n agency: { type: Schema.Types.ObjectId, required: true },\n});\n\nexport const SentFileModel = mongoose.model(\"SentFile\", sentFileSchema);\n\ntype SentFile = typeof SentFileModel extends Model\u003cinfer Raw> ? Raw : never;\n\nexport const readAgency = (file: SentFile) => file.agency;\n\nexport const historySchema = new Schema\u003c{ updatedAt: Date }>({ updatedAt: Date }, { _id: false });\n```\n\n```sh\nnpm i mongoose@9.11.2 @types/node typescript\nnpx tsc --noEmit --strict --module nodenext --target es2022 --skipLibCheck --types node first.ts\nnpx tsc --noEmit --strict --module nodenext --target es2022 --skipLibCheck --types node last.ts\n```\n\n### 🙁 Actual behavior\n\n```\nfirst.ts(13,52): error TS2339: Property 'agency' does not exist on type 'never'.\n```\n\n`last.ts` reports no error. Removing the `{ _id: false }` argument of `historySchema` in `first.ts` also makes the error go away.\n\n### 🙂 Expected behavior\n\nThe same diagnostics whatever the order of the two independent declarations. Either answer could be the intended one; the point is that it changes with check order.\n\n### Additional information about the issue\n\n- Assigning a similar model (same schema plus `timestamps` and a subdocument) to `Model\u003cany>` fails on 7.0.2: `Model\u003c{ agency: ObjectId; … }, {}, {}, { id: string }, …>` is not assignable to `Model\u003cany, {}, {}, {}, any, any, any>`, the elaboration ending in `The 'this' types of each signature are incompatible` / `Property 'id' is missing in type '{}' but required in type '{ id: string; }'` on `schema.virtuals`. So whether `Model` is related through its type arguments or structurally seems to depend on what an earlier statement left in the checker's caches.\n- Related: #56099 has the same shape without mongoose (an unused interface changes the result of `X extends C\u003cinfer V>`), still open. On the mongoose side, Automattic/mongoose#16503 reports that since 9.10.0 a model with typed virtuals is not assignable to `Model\u003cany>`; the maintainer observed it only on TypeScript 7 with `skipLibCheck`, never on 5.9, which fits a result that depends on check order. Their fix (Automattic/mongoose#16505) relaxed the populate typings only, so the relation itself still fails on 9.11.2.\n- In a real multi-file project the effect is order of files, not statements: the error appears or disappears depending on which file a checker visits first. With TypeScript 7's parallel checkers, that turns into diagnostics that change with `--checkers` and with how files are split across checkers. On a ~9,500-file project we see a few dozen such errors (this TS2339, TS2344 \"`Model\u003c…>` does not satisfy the constraint `Model\u003cany, {}, {}, {}, any, any, any>`\", and values typed `never`) appear only under some file orders, and none in the default order.\n\n","author":{"url":"https://github.com/jltrv","@type":"Person","name":"jltrv"},"datePublished":"2026-11-20T17:02:58Z","url":"https://github.com/microsoft/TypeScript/issues/65607"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:57725467-60b1-4bdf-e0df-e7f091bde094 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 95E2:31E99C:8295C9:B08743:6B00E10D |
| html-safe-nonce | a7b42fd068a79322daffa36db911f8cdf248e5ec918b00059a37baa7e5cab0cd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUUyOjMxRTk5Qzo4Mjk1Qzk6QjA4NzQzOjZCMDBFMTBEIiwidmlzaXRvcl9pZCI6Ijg4MTA3NDczOTY1ODI1OTY4NzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9602766146d6955500bdc848534abe2d3fee92922523e923ad190c004cd4f8a2 |
| hovercard-subject-tag | issue:6516019756 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/65607/issue_layout |
| twitter:image | https://opengraph.githubassets.com/90ba6ccbc41a05c8532f65772d958d4a6033844226e96cbcc7ef78cf7ec4dc37/microsoft/TypeScript/issues/65607 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90ba6ccbc41a05c8532f65772d958d4a6033844226e96cbcc7ef78cf7ec4dc37/microsoft/TypeScript/issues/65607 |
| og:image:alt | 🔎 Search Terms check order, declaration order, statement order, conditional type infer never, mongoose Model, history dependent diagnostics, checkers 🕗 Version & Regression Information Reproduces i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jltrv |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width