使用backbone.d.ts(Using a backbone.d.ts)

编程入门 行业动态 更新时间:2024-10-15 18:22:46
使用backbone.d.ts(Using a backbone.d.ts)

当模块没有被定义为字符串时,我似乎无法获得任何导入工作。 到底是怎么回事?

test.ts

import b = module('Backbone')

不起作用backbone.d.ts

declare module Backbone { export class Events { ...

作品backbone.d.ts

declare module "Backbone" { export class Events { ...

编辑1

供参考10.1.4

带有StringLiteral的AmbientModuleIdentification声明一个外部模块。 这种类型的声明只能在全局模块中使用。 StringLiteral必须指定顶级外部模块名称。 不允许使用相对外部模块名称

我不明白如何将它指定为在这里和这里找到的字符串字面格式是非常有用的。 它可以工作,如果你使用///<reference...没有字符串文字模块,但我试图生成依赖这些库的AMD模块,所以我需要导入工作。 我是否是少数派,我必须去修改每个.d.ts作为字符串文字版本?

编辑2

使用字符串文字声明一个模块要求你的导入是完全匹配的,如果该字符串文字。 即使该模块与模块定义的位置不在相同的目录中,也不能再使用相对或绝对路径来定位该模块/模块定义。 这使得///<reference和导入是必需的,但是产生了一个模块,它带有正在查找的tsc --module AMD (模块的路径是由模块字符串文字"Backbone" )。

例如。

+- dep/ |- backbone.d.ts |- test.ts

backbone.d.ts

declare module "Backbone" { export class Events {

作品test.ts

///<reference path="../dep/backbone.d.ts" /> import b = module('Backbone') // generates // define(["require", "exports", 'Backbone']

不起作用test.ts

import b = module('./dep/Backbone')

请注意,这也适用于...

declare module "libs/Backbone" { ... ///<reference path="dep/backbone.d.ts" /> import b = module('libs/Backbone') ... // generates define(["require", "exports", 'libs/Backbone']

I can't seem to get any import to work when the module isn't defined as a string. What is going on?

test.ts

import b = module('Backbone')

Does not work: backbone.d.ts

declare module Backbone { export class Events { ...

Works: backbone.d.ts

declare module "Backbone" { export class Events { ...

Edit 1:

FYI From 10.1.4

An AmbientModuleIdentification with a StringLiteral declares an external module. This type of declaration is permitted only in the global module. The StringLiteral must specify a top-level external module name. Relative external module names are not permitted

I don't understand how it's useful to not specify it as the string literal format as found here and here. It works if you use ///<reference... without a string literal module but I'm trying to generate AMD modules that depend on these libraries so I need the import to work. Am I the minority and I have to go and modify each .d.ts to be the string literal version?

Edit 2:

Declaring a module using a string literal requires that your import is an exact match if that string literal. You can no longer use relative or absolute paths to the location of this module/module definition even if it is not located in the same directory as the file trying to import it. This makes it that a ///<reference and an import are required but produces a module with tsc --module AMD that was exactly looking for (path to module is as dictated by the module string literal "Backbone").

For example.

+- dep/ |- backbone.d.ts |- test.ts

backbone.d.ts:

declare module "Backbone" { export class Events {

Works: test.ts:

///<reference path="../dep/backbone.d.ts" /> import b = module('Backbone') // generates // define(["require", "exports", 'Backbone']

Does not work: test.ts:

import b = module('./dep/Backbone')

Note that this works as well...

declare module "libs/Backbone" { ... ///<reference path="dep/backbone.d.ts" /> import b = module('libs/Backbone') ... // generates define(["require", "exports", 'libs/Backbone']

最满意答案

当你写这个:

declare module Backbone {

这意味着你已经拥有一个处于全局范围内的模块(所以你可以毫不费力地使用它,不需要导入)。 但是,当你写这个:

declare module "Backbone" {

这意味着你指定了导入模块(import ... = module(“...”))的外观。

When you write this:

declare module Backbone {

It means you have already a module which is in the global scope (so you can immeadiately use it, no import is required). But when you write this:

declare module "Backbone" {

It means you specify how the imported module (import ... = module("...")) will look.

更多推荐

本文发布于:2023-08-03 09:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:backbone   ts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!