GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Adds support for "typesVersions" redirects by rbuckton · Pull Request #26568 · microsoft/TypeScript

Conversation

@rbuckton

@rbuckton Ron Buckton (rbuckton) commented Aug 21, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This adds support TypeScript version-specific redirects to type definitions in a package. If a package wishes to include type definitions that gracefully downgrade to older editions of TypeScript, they will be able to do so by adding the following entry to their package.json file:

{
  "name": "package-name",
  "version": "1.0",
  "types": "index",
  "typesVersions": {
    ">=3.1.0-0": { "*": ["ts3.1/*"] }
  }
}

When a "typesVersions" field is found in a referenced package, we search for the first version range that matches the current compiler version. If a match is found, the associated path patterns are applied.

Version ranges specified in "typesVersions" keys follow the same range comparator format (strict only) as https://github.com/npm/node-semver#ranges.

Path patterns are applied in the same way that the "paths" compiler option is applied to paths in your project.

This feature will only support compiler version of 3.1 or later. Earlier TypeScript compilers will use the existing behavior.

Fixes #22605

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

wsmd and sheerlox reacted with heart emoji
All reactions
  • ❤️ 2 reactions
Ron Buckton (rbuckton) added 3 commits August 20, 2018 16:57
@rbuckton
Initial support for 'typesVersions'
015babb
@rbuckton
Added fourslash test
6f7a37c
@rbuckton
Base version test on 'versionMajorMinor'
8398a87
@rbuckton
Ron Buckton (rbuckton) requested review from Ryan Cavanaugh (RyanCavanaugh) and Wesley Wigham (weswigham) August 21, 2018 00:59
@rbuckton

Ron Buckton (rbuckton) commented Aug 21, 2018

Copy link
Copy Markdown
Contributor Author

There is some overlap between src/compiler/semver.ts and src/jsTyping/semver.ts. I may retire the version in jsTyping as its very specific to its implementation, while the implementation in compiler is more generic and can serve both cases. I can either address this in this PR or a follow up.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Aug 21, 2018

Copy link
Copy Markdown
Member

If a match is found, the value is inserted into the path between the package directory and "types" (or any named submodule beneath the package).

This feels strange. I'd expect either a module specifier (so it points directly at another package to defer to), or a direct path.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton

Ron Buckton (rbuckton) commented Aug 21, 2018

Copy link
Copy Markdown
Contributor Author

This is more like a per-version root directory override. There are two kinds of typed packages we need to support:

  • Ambient module based packages (that generally have types defined in a single file like "index.d.ts")
  • Non-ambient module based packages (that use top level exports and use multiple files)

See https://gist.github.com/rbuckton/bcde1c2adde270c3b0d7ced5320c0877 for a more detailed description and examples.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton
Adjust subModuleName
aa04ef5
@rbuckton

Ron Buckton (rbuckton) commented Aug 21, 2018

Copy link
Copy Markdown
Contributor Author

[…] I'd expect either a module specifier (so it points directly at another package to defer to), or a direct path.

Pointing to another package is not the intended scenario. This is intended to support the following two scenarios:

  1. Declarations defined via ambient modules in a single index.d.ts file (common on DefinitelyTyped)
  2. Declarations defined via one or more declaration files using top level exports (common on DefinitelyTyped and default behavior of TypeScript build outputs).

If it were a direct path, we could not support (2). We could consider some kind of path mapping/path template, but overall it would just add unnecessary complexity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton

Ron Buckton (rbuckton) commented Aug 21, 2018

Copy link
Copy Markdown
Contributor Author

I have a few open questions:

  • Should we ignore paths that traverse outside of the package (e.g. disallow absolute paths or "..")?
  • Should we compare against TypeScript's versionMajorMinor or version? While version seems to be more correct, due to the precedence rules of semver, 3.1 comes after 3.1.0-dev/3.1.0-next, so using a simple version number like 3.1 wouldn't work when running a dev/next/insiders build.
  • Should we consider a path templating feature, or leave it as a prefix path only?
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton
Merge branch 'master' into typesVersions
79d7f37
@rbuckton

Ron Buckton (rbuckton) commented Aug 22, 2018

Copy link
Copy Markdown
Contributor Author

If we decide we want to support semver ranges in "typesVersions", I actually was able to put together a relatively lightweight semver range parser based on the BNF here: https://github.com/npm/node-semver#range-grammar

I put a copy of the version of semver.ts with support for ranges here in a gist so as not to pollute this PR with this change unless we decide to implement it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton
Ron Buckton (rbuckton) force-pushed the typesVersions branch 6 times, most recently from d350e7b to e02dd34 Compare August 22, 2018 18:38
@rbuckton
Remove jsTypings/semver.ts, add unit tests and logging
b9afdad
@rbuckton
Ron Buckton (rbuckton) force-pushed the typesVersions branch from e02dd34 to b9afdad Compare August 22, 2018 19:38
@rbuckton Ron Buckton (rbuckton) mentioned this pull request Aug 22, 2018
Use unique symbol for well-known symbols #21603
Closed
@rbuckton

Ron Buckton (rbuckton) commented Aug 23, 2018

Copy link
Copy Markdown
Contributor Author

Should we consider a path templating feature, or leave it as a prefix path only?

If we want to leverage our existing path templating functionality, it would change "typesVersions" to look like this:

{
  "typesVersions": {
    "3.1": { "*": ["ts3.1/*"] }
  }
}

This would also require refactoring tryLoadModuleUsingBaseUrl to extract its logic and inject it in various places in moduleNameResolver.ts.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

weswigham
Wesley Wigham (weswigham) requested changes Aug 23, 2018
View reviewed changes

@weswigham Wesley Wigham (weswigham) left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs some declaration emit tests that write out paths (and maybe import quick fixes), ie.

// @declaration: true
// @filename: node_modules/foo/tsnew/index.d.ts
export interface Foo {}
export function foo(): Foo;
// @filename: node_modules/foo/tsnew/deep.d.ts
export interface Foo2 {}
export function foo2(): Foo2;
// @filename: node_modules/foo/index.d.ts
export interface Bar {}
export function bar(): Bar;
// @filename: node_modules/foo/deep.d.ts
export interface Bar2 {}
export function bar2(): Bar2;
// @filename: node_modules/foo/package.json
{
  "name": "foo",
  "version": 0.0.1,
  "types": "index.d.ts",
  "typesVersions": { "3.1": "tsnew/" }
}
// @filename: consumer.ts
import { foo } from "foo";
import { foo2 } from "foo/deep";
export const v = foo(); // will use an import type to name this in declaration emit
export const v2 = foo2();

Just because the moduleSpecifiers.ts file is separate from the resolver so I have low confidence that resolution changes are instantly reflected in declaration files.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@weswigham

Wesley Wigham (weswigham) commented Aug 23, 2018

Copy link
Copy Markdown
Member

Ah! Also a test of what we do when a package's "latest version" root file attempts to import the real root, eg

// @declaration: true
// @filename: node_modules/bar/tsnew/index.d.ts
export * from "../";
// @filename: node_modules/bar/tsnew/deep.d.ts
export * from "../deep";
// @filename: node_modules/bar/index.d.ts
export interface Bar {}
export function bar(): Bar;
// @filename: node_modules/bar/deep.d.ts
export interface Bar2 {}
export function bar2(): Bar2;
// @filename: node_modules/bar/package.json
{
  "name": "bar",
  "version": 0.0.1,
  "types": "index.d.ts",
  "typesVersions": { "3.1": "tsnew/" }
}
// @filename: consumer.ts
import { bar } from "bar";
import { bar2 } from "bar/deep";
export const v = bar();
export const v2 = bar2();
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Aug 23, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Also, I know I've said this in person, but do we know of any non-DT people we're writing this for? Because on DT this is all going to be managed by the gen script, so no human hand should ever touch these fields - meaning a pretty or simple configuration isn't required; a manifest of real paths to redirects would even be sufficient, eg

{
  "typesVersions": {
    "3.1": {
      "./index.d.ts": "./ts3.1/index.d.ts",
      "./deep.d.ts": "./ts3.1/deep.d.ts",
      "./sub/module/index.d.ts": "./ts3.1/sub/module/index.d.ts",
    }
  }
}

which avoids encoding the folder-only-redirects convention into the feature without going full path-mapping. If we know of some others who'd like to use this, it'd be great to get their input on what kind of configuration makes the most sense from the perspective of their build tools and source trees.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ajafff

Klaus Meinhardt (ajafff) commented Aug 23, 2018

Copy link
Copy Markdown
Contributor

do we know of any non-DT people we're writing this for?

👋
I need this in tsutils to expose an API that is compatible with the TypeScript version you are compiling with.

Regarding submodule imports:
When importing from tsutils/util does it even look at tsutils/package.json? Node's module spec says it doesn't.

However, I want to redirect imports from package root as well as imports from any submodule:

  • tsutils -> tsutils/${tsVersion}
  • tsutils/typeguard/node -> tsutils/${tsVersion}/typeguard/node
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Aug 24, 2018

Copy link
Copy Markdown
Member

Also, I know I've said this in person, but do we know of any non-DT people we're writing this for?

Dojo and Angular have both voiced complaints about the inability for them to bundle typings of varying versions. They still have the problem of how to generate downlevel-compatible .d.ts files but that's sort of a separate issue.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton

Ron Buckton (rbuckton) commented Aug 27, 2018

Copy link
Copy Markdown
Contributor Author

If we support path mappings, would we want completions to include both mapped and unmapped paths?

Consider the directory structure for node_modules/foo:

package.json
index.d.ts
aaa.d.ts
ts3.1/index.d.ts
ts3.1/zzz.d.ts

With the following in package.json:

{
  "typesVersions": {
    "3.1": { "*": ["ts3.1/*"] }
  }
}

If we follow the same logic for completions that we do for baseUrl/paths, then a completion for "foo/ would return index, aaa, ts3.1 (directory completion), and zzz (from path mapping).

Does that seem acceptable?

weswigham reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton
Switch to paths-like pattern matching
dc5cd96
@rbuckton
Merge branch 'master' into typesVersions
210de32
@rbuckton

Ron Buckton (rbuckton) commented Aug 29, 2018

Copy link
Copy Markdown
Contributor Author

Wesley Wigham (@weswigham) can you take another look?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rbuckton
Ron Buckton (rbuckton) requested review from a user and Nathan Shively-Sanders (sandersn) August 30, 2018 18:28
RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) reviewed Sep 5, 2018
View reviewed changes
Comment thread src/compiler/moduleNameResolver.ts
if (!hasProperty(typesVersions, key)) continue;

const keyRange = VersionRange.tryParse(key);
if (keyRange === undefined) {

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) Sep 5, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case probably deserves a log message since it's basically a silent error

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rbuckton Ron Buckton (rbuckton) Sep 5, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a verification pass to readPackageJsonTypesVersionPaths, since getPackageJsonTypesVersionsPaths is also used by services and doesn't take a ModuleResolutionState argument.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) requested a review from Sheetal Nandi (sheetalkamat) September 5, 2018 17:46
weswigham
Wesley Wigham (weswigham) approved these changes Sep 5, 2018
View reviewed changes
@rbuckton
PR Feedback
e726e4c
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Sep 7, 2018
Design Meeting Notes, 8/31/2018 #26954
Closed
@rbuckton
Merge branch 'master' into typesVersions
a255d9a
RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) approved these changes Sep 7, 2018
View reviewed changes
@rbuckton
Ron Buckton (rbuckton) merged commit af8e44a into master Sep 7, 2018
@rbuckton
Ron Buckton (rbuckton) deleted the typesVersions branch September 7, 2018 22:40
@gcanti Giulio Canti (gcanti) mentioned this pull request Sep 7, 2018
Use unknown type instead of {} (TypeScript 3.0+) gcanti/fp-ts#539
Closed
@Bnaya Bnaya Peretz (Bnaya) mentioned this pull request Sep 8, 2018
Support older versions of angular/typescript: hirezio/auto-spies#7
Closed
This was referenced Oct 8, 2018
@types/[email protected] throws Error: Cannot find name 'Set' DefinitelyTyped/DefinitelyTyped#28969
Closed
Fix return type of NodeJS.ReadableStream#read DefinitelyTyped/DefinitelyTyped#26373
Closed
[jquery] Accept union types for the call signature of JQueryStatic. DefinitelyTyped/DefinitelyTyped#29612
Closed
@argv-minus-one argv-minus-one mentioned this pull request Nov 24, 2018
node: make streams generic DefinitelyTyped/DefinitelyTyped#27346
Closed
9 tasks done
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request Dec 16, 2018
Definitely Not Versioned DefinitelyTyped/DefinitelyTyped#7719
Closed
@johnrom John Rom (johnrom) mentioned this pull request Jun 10, 2019
TypeScript Compatibility - Improving back-compat or defining supported versions jaredpalmer/formik#1598
Closed
@cordiosd

cordiosd commented Jun 15, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

#31907

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham Wesley Wigham (weswigham) mentioned this pull request Aug 13, 2019
Relative path reference for declaration #26439
Closed
@sseppola Sindre Seppola (sseppola) mentioned this pull request May 11, 2020
[yup] Make all schemas default to being typed as optional (notRequired) DefinitelyTyped/DefinitelyTyped#44589
Merged
10 tasks done
@chadoh Chad Ostrowski (chadoh) mentioned this pull request Jul 22, 2020
When near-api-js updates TypeScript dependency, my project breaks near/near-api-js#368
Closed
@johnrom John Rom (johnrom) mentioned this pull request Apr 17, 2021
Strongly Typed Fields jaredpalmer/formik#1334
Open
@jaylinski Jay Linski (jaylinski) mentioned this pull request Jan 7, 2022
Use automatically generated types handlebars-lang/handlebars.js#1817
Closed
2 tasks
@rmeissner Richard Meissner (rmeissner) mentioned this pull request Feb 8, 2022
Building project with tsc fails due to error in Ganache typings ConsenSys-archive/ganache#2300
Closed
@ajuanjojjj Juanjo (ajuanjojjj) mentioned this pull request Jul 28, 2023
Rename FileSystem interface to CDVFileSystem apache/cordova-plugin-file#588
Open
4 of 5 tasks
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@weswigham Wesley Wigham (weswigham) weswigham approved these changes
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) RyanCavanaugh approved these changes
@sandersn Nathan Shively-Sanders (sandersn) Awaiting requested review from sandersn
@sheetalkamat Sheetal Nandi (sheetalkamat) Awaiting requested review from sheetalkamat

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Provide a back-compat mechanism for bundled .d.ts files in package.json

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@rbuckton @weswigham @ajafff @RyanCavanaugh @cordiosd @sheetalkamat
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.