mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 21:36:50 -05:00
Allow untyped imports
This commit is contained in:
37
tests/baselines/reference/untypedModuleImport.js
Normal file
37
tests/baselines/reference/untypedModuleImport.js
Normal file
@@ -0,0 +1,37 @@
|
||||
//// [tests/cases/conformance/moduleResolution/untypedModuleImport.ts] ////
|
||||
|
||||
//// [index.js]
|
||||
// This tests that importing from a JS file globally works in an untyped way.
|
||||
// (Assuming we don't have `--noImplicitAny` or `--allowJs`.)
|
||||
|
||||
This file is not processed.
|
||||
|
||||
//// [a.ts]
|
||||
import * as foo from "foo";
|
||||
foo.bar();
|
||||
|
||||
//// [b.ts]
|
||||
import foo = require("foo");
|
||||
foo();
|
||||
|
||||
//// [c.ts]
|
||||
import foo, { bar } from "foo";
|
||||
import "./a";
|
||||
import "./b";
|
||||
foo(bar());
|
||||
|
||||
|
||||
//// [a.js]
|
||||
"use strict";
|
||||
var foo = require("foo");
|
||||
foo.bar();
|
||||
//// [b.js]
|
||||
"use strict";
|
||||
var foo = require("foo");
|
||||
foo();
|
||||
//// [c.js]
|
||||
"use strict";
|
||||
var foo_1 = require("foo");
|
||||
require("./a");
|
||||
require("./b");
|
||||
foo_1["default"](foo_1.bar());
|
||||
25
tests/baselines/reference/untypedModuleImport.symbols
Normal file
25
tests/baselines/reference/untypedModuleImport.symbols
Normal file
@@ -0,0 +1,25 @@
|
||||
=== /c.ts ===
|
||||
import foo, { bar } from "foo";
|
||||
>foo : Symbol(foo, Decl(c.ts, 0, 6))
|
||||
>bar : Symbol(bar, Decl(c.ts, 0, 13))
|
||||
|
||||
import "./a";
|
||||
import "./b";
|
||||
foo(bar());
|
||||
>foo : Symbol(foo, Decl(c.ts, 0, 6))
|
||||
>bar : Symbol(bar, Decl(c.ts, 0, 13))
|
||||
|
||||
=== /a.ts ===
|
||||
import * as foo from "foo";
|
||||
>foo : Symbol(foo, Decl(a.ts, 0, 6))
|
||||
|
||||
foo.bar();
|
||||
>foo : Symbol(foo, Decl(a.ts, 0, 6))
|
||||
|
||||
=== /b.ts ===
|
||||
import foo = require("foo");
|
||||
>foo : Symbol(foo, Decl(b.ts, 0, 0))
|
||||
|
||||
foo();
|
||||
>foo : Symbol(foo, Decl(b.ts, 0, 0))
|
||||
|
||||
31
tests/baselines/reference/untypedModuleImport.types
Normal file
31
tests/baselines/reference/untypedModuleImport.types
Normal file
@@ -0,0 +1,31 @@
|
||||
=== /c.ts ===
|
||||
import foo, { bar } from "foo";
|
||||
>foo : any
|
||||
>bar : any
|
||||
|
||||
import "./a";
|
||||
import "./b";
|
||||
foo(bar());
|
||||
>foo(bar()) : any
|
||||
>foo : any
|
||||
>bar() : any
|
||||
>bar : any
|
||||
|
||||
=== /a.ts ===
|
||||
import * as foo from "foo";
|
||||
>foo : any
|
||||
|
||||
foo.bar();
|
||||
>foo.bar() : any
|
||||
>foo.bar : any
|
||||
>foo : any
|
||||
>bar : any
|
||||
|
||||
=== /b.ts ===
|
||||
import foo = require("foo");
|
||||
>foo : any
|
||||
|
||||
foo();
|
||||
>foo() : any
|
||||
>foo : any
|
||||
|
||||
16
tests/baselines/reference/untypedModuleImport_allowJs.js
Normal file
16
tests/baselines/reference/untypedModuleImport_allowJs.js
Normal file
@@ -0,0 +1,16 @@
|
||||
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_allowJs.ts] ////
|
||||
|
||||
//// [index.js]
|
||||
// Same as untypedModuleImport.ts but with --allowJs, so the package will actually be typed.
|
||||
|
||||
exports.default = { bar() { return 0; } }
|
||||
|
||||
//// [a.ts]
|
||||
import foo from "foo";
|
||||
foo.bar();
|
||||
|
||||
|
||||
//// [a.js]
|
||||
"use strict";
|
||||
var foo_1 = require("foo");
|
||||
foo_1["default"].bar();
|
||||
@@ -0,0 +1,17 @@
|
||||
=== /a.ts ===
|
||||
import foo from "foo";
|
||||
>foo : Symbol(foo, Decl(a.ts, 0, 6))
|
||||
|
||||
foo.bar();
|
||||
>foo.bar : Symbol(bar, Decl(index.js, 2, 19))
|
||||
>foo : Symbol(foo, Decl(a.ts, 0, 6))
|
||||
>bar : Symbol(bar, Decl(index.js, 2, 19))
|
||||
|
||||
=== /node_modules/foo/index.js ===
|
||||
// Same as untypedModuleImport.ts but with --allowJs, so the package will actually be typed.
|
||||
|
||||
exports.default = { bar() { return 0; } }
|
||||
>exports : Symbol(default, Decl(index.js, 0, 0))
|
||||
>default : Symbol(default, Decl(index.js, 0, 0))
|
||||
>bar : Symbol(bar, Decl(index.js, 2, 19))
|
||||
|
||||
22
tests/baselines/reference/untypedModuleImport_allowJs.types
Normal file
22
tests/baselines/reference/untypedModuleImport_allowJs.types
Normal file
@@ -0,0 +1,22 @@
|
||||
=== /a.ts ===
|
||||
import foo from "foo";
|
||||
>foo : { bar(): number; }
|
||||
|
||||
foo.bar();
|
||||
>foo.bar() : number
|
||||
>foo.bar : () => number
|
||||
>foo : { bar(): number; }
|
||||
>bar : () => number
|
||||
|
||||
=== /node_modules/foo/index.js ===
|
||||
// Same as untypedModuleImport.ts but with --allowJs, so the package will actually be typed.
|
||||
|
||||
exports.default = { bar() { return 0; } }
|
||||
>exports.default = { bar() { return 0; } } : { bar(): number; }
|
||||
>exports.default : any
|
||||
>exports : any
|
||||
>default : any
|
||||
>{ bar() { return 0; } } : { bar(): number; }
|
||||
>bar : () => number
|
||||
>0 : 0
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/a.ts(1,22): error TS6144: A package for 'foo' was found at '/node_modules/foo/index.js', but is untyped. Because '--noImplicitAny' is enabled, this package must have a declaration.
|
||||
|
||||
|
||||
==== /a.ts (1 errors) ====
|
||||
import * as foo from "foo";
|
||||
~~~~~
|
||||
!!! error TS6144: A package for 'foo' was found at '/node_modules/foo/index.js', but is untyped. Because '--noImplicitAny' is enabled, this package must have a declaration.
|
||||
|
||||
==== /node_modules/foo/index.js (0 errors) ====
|
||||
// This tests that `--noImplicitAny` disables untyped modules.
|
||||
|
||||
This file is not processed.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_noImplicitAny.ts] ////
|
||||
|
||||
//// [index.js]
|
||||
// This tests that `--noImplicitAny` disables untyped modules.
|
||||
|
||||
This file is not processed.
|
||||
|
||||
//// [a.ts]
|
||||
import * as foo from "foo";
|
||||
|
||||
|
||||
//// [a.js]
|
||||
"use strict";
|
||||
@@ -0,0 +1,13 @@
|
||||
/a.ts(1,22): error TS6143: Module './foo' was resolved to '/foo.js', but '--allowJs' is not set.
|
||||
|
||||
|
||||
==== /a.ts (1 errors) ====
|
||||
import * as foo from "./foo";
|
||||
~~~~~~~
|
||||
!!! error TS6143: Module './foo' was resolved to '/foo.js', but '--allowJs' is not set.
|
||||
|
||||
==== /foo.js (0 errors) ====
|
||||
// This tests that untyped module imports don't happen with local imports.
|
||||
|
||||
This file is not processed.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_noLocalImports.ts] ////
|
||||
|
||||
//// [foo.js]
|
||||
// This tests that untyped module imports don't happen with local imports.
|
||||
|
||||
This file is not processed.
|
||||
|
||||
//// [a.ts]
|
||||
import * as foo from "./foo";
|
||||
|
||||
|
||||
//// [a.js]
|
||||
"use strict";
|
||||
23
tests/baselines/reference/untypedModuleImport_vsAmbient.js
Normal file
23
tests/baselines/reference/untypedModuleImport_vsAmbient.js
Normal file
@@ -0,0 +1,23 @@
|
||||
//// [tests/cases/conformance/moduleResolution/untypedModuleImport_vsAmbient.ts] ////
|
||||
|
||||
//// [index.js]
|
||||
// This tests that an ambient module declaration overrides an untyped import.
|
||||
|
||||
This file is not processed.
|
||||
|
||||
//// [declarations.d.ts]
|
||||
declare module "foo" {
|
||||
export const x: number;
|
||||
}
|
||||
|
||||
//// [a.ts]
|
||||
/// <reference path="./declarations.d.ts" />
|
||||
import { x } from "foo";
|
||||
x;
|
||||
|
||||
|
||||
//// [a.js]
|
||||
"use strict";
|
||||
/// <reference path="./declarations.d.ts" />
|
||||
var foo_1 = require("foo");
|
||||
foo_1.x;
|
||||
@@ -0,0 +1,14 @@
|
||||
=== /a.ts ===
|
||||
/// <reference path="./declarations.d.ts" />
|
||||
import { x } from "foo";
|
||||
>x : Symbol(x, Decl(a.ts, 1, 8))
|
||||
|
||||
x;
|
||||
>x : Symbol(x, Decl(a.ts, 1, 8))
|
||||
|
||||
=== /declarations.d.ts ===
|
||||
declare module "foo" {
|
||||
export const x: number;
|
||||
>x : Symbol(x, Decl(declarations.d.ts, 1, 16))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
=== /a.ts ===
|
||||
/// <reference path="./declarations.d.ts" />
|
||||
import { x } from "foo";
|
||||
>x : number
|
||||
|
||||
x;
|
||||
>x : number
|
||||
|
||||
=== /declarations.d.ts ===
|
||||
declare module "foo" {
|
||||
export const x: number;
|
||||
>x : number
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// @noImplicitReferences: true
|
||||
// @currentDirectory: /
|
||||
// This tests that importing from a JS file globally works in an untyped way.
|
||||
// (Assuming we don't have `--noImplicitAny` or `--allowJs`.)
|
||||
|
||||
// @filename: /node_modules/foo/index.js
|
||||
This file is not processed.
|
||||
|
||||
// @filename: /a.ts
|
||||
import * as foo from "foo";
|
||||
foo.bar();
|
||||
|
||||
// @filename: /b.ts
|
||||
import foo = require("foo");
|
||||
foo();
|
||||
|
||||
// @filename: /c.ts
|
||||
import foo, { bar } from "foo";
|
||||
import "./a";
|
||||
import "./b";
|
||||
foo(bar());
|
||||
@@ -0,0 +1,12 @@
|
||||
// @noImplicitReferences: true
|
||||
// @currentDirectory: /
|
||||
// @allowJs: true
|
||||
// @maxNodeModuleJsDepth: 1
|
||||
// Same as untypedModuleImport.ts but with --allowJs, so the package will actually be typed.
|
||||
|
||||
// @filename: /node_modules/foo/index.js
|
||||
exports.default = { bar() { return 0; } }
|
||||
|
||||
// @filename: /a.ts
|
||||
import foo from "foo";
|
||||
foo.bar();
|
||||
@@ -0,0 +1,10 @@
|
||||
// @noImplicitReferences: true
|
||||
// @currentDirectory: /
|
||||
// @noImplicitAny: true
|
||||
// This tests that `--noImplicitAny` disables untyped modules.
|
||||
|
||||
// @filename: /node_modules/foo/index.js
|
||||
This file is not processed.
|
||||
|
||||
// @filename: /a.ts
|
||||
import * as foo from "foo";
|
||||
@@ -0,0 +1,9 @@
|
||||
// @noImplicitReferences: true
|
||||
// @currentDirectory: /
|
||||
// This tests that untyped module imports don't happen with local imports.
|
||||
|
||||
// @filename: /foo.js
|
||||
This file is not processed.
|
||||
|
||||
// @filename: /a.ts
|
||||
import * as foo from "./foo";
|
||||
@@ -0,0 +1,16 @@
|
||||
// @noImplicitReferences: true
|
||||
// @currentDirectory: /
|
||||
// This tests that an ambient module declaration overrides an untyped import.
|
||||
|
||||
// @filename: /node_modules/foo/index.js
|
||||
This file is not processed.
|
||||
|
||||
// @filename: /declarations.d.ts
|
||||
declare module "foo" {
|
||||
export const x: number;
|
||||
}
|
||||
|
||||
// @filename: /a.ts
|
||||
/// <reference path="./declarations.d.ts" />
|
||||
import { x } from "foo";
|
||||
x;
|
||||
22
tests/cases/fourslash/untypedModuleImport.ts
Normal file
22
tests/cases/fourslash/untypedModuleImport.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @Filename: node_modules/foo/index.js
|
||||
////{}
|
||||
|
||||
// @Filename: a.ts
|
||||
////import /*foo*/[|foo|] from /*fooModule*/"foo";
|
||||
////[|foo|]();
|
||||
|
||||
goTo.file("a.ts");
|
||||
debug.printErrorList();
|
||||
verify.numberOfErrorsInCurrentFile(0);
|
||||
|
||||
goTo.marker("fooModule");
|
||||
verify.goToDefinitionIs([]);
|
||||
verify.quickInfoIs('module "foo"');
|
||||
verify.referencesAre([])
|
||||
|
||||
goTo.marker("foo");
|
||||
verify.goToDefinitionIs([]);
|
||||
verify.quickInfoIs("import foo");
|
||||
verify.rangesReferenceEachOther();
|
||||
Reference in New Issue
Block a user