我在我的TypeScript项目中有一个声明文件,如下所示:
// myapp.d.ts
declare namespace MyApp {
interface MyThing {
prop1: string
prop2: number
}
}
Run Code Online (Sandbox Code Playgroud)
这很好用,我可以在项目的任何地方使用这个命名空间,而无需导入它.
我现在需要从第三方模块导入一个类型并在我的环境声明中使用它:
// myapp.d.ts
import {SomeType} from 'module'
declare namespace MyApp {
interface MyThing {
prop1: string
prop2: number
prop3: SomeType
}
}
Run Code Online (Sandbox Code Playgroud)
编译器现在抱怨它找不到命名空间'MyApp',大概是因为导入阻止它成为环境.
在使用第三方类型时,是否有一些简单的方法可以保留声明的环境效果?
这个问题已被问过几次,但给出的答案都没有对我有用。我正在尝试扩展 ExpressRequest对象以包含存储User对象的属性。我创建了一个声明文件,express.d.ts并将它放在与我的相同的目录中tsconfig.json:
import { User } from "./src/models/user";
declare namespace Express {
export interface Request {
user: User;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试对其进行赋值secured-api.ts:
import express from 'express';
import { userService } from '../services/user';
router.use(async (req, res, next) => {
try {
const user = await userService.findByUsername(payload.username);
// do stuff to user...
req.user = user;
next();
} catch(err) {
// handle error
}
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
src/routes/secured-api.ts:38:21 - error TS2339: Property 'user' does not …Run Code Online (Sandbox Code Playgroud) 我在 preload.js 中定义了 contextBridge ( https://www.electronjs.org/docs/all#contextbridge ),如下所示:
const {
contextBridge,
ipcRenderer
} = require("electron")
contextBridge.exposeInMainWorld(
"api", {
send: (channel, data) => {
ipcRenderer.invoke(channel, data).catch(e => console.log(e))
},
receive: (channel, func) => {
console.log("preload-receive called. args: ");
ipcRenderer.on(channel, (event, ...args) => func(...args));
},
// https://www.electronjs.org/docs/all#ipcrenderersendtowebcontentsid-channel-args
electronIpcSendTo: (window_id: string, channel: string, ...arg: any) => {
ipcRenderer.sendTo(window_id, channel, arg);
},
// https://github.com/frederiksen/angular-electron-boilerplate/blob/master/src/preload
/preload.ts
electronIpcSend: (channel: string, ...arg: any) => {
ipcRenderer.send(channel, arg);
},
electronIpcSendSync: (channel: string, ...arg: any) => {
return ipcRenderer.sendSync(channel, arg); …Run Code Online (Sandbox Code Playgroud) 我已经在 .d.ts 中声明了命名空间,它应该是全局可用的,如下所示:
/src/typings/content.d.ts
import * as something from 'something';
declare namespace Content {
...
type Object = internal.Object;
}
Run Code Online (Sandbox Code Playgroud)
我想将此命名空间用于 TSX 文件中的接口:
import * as React from 'react';
interface ExampleComponentProps {
example: Content.Object;
}
export const ExampleComponent: React.FunctionComponent<ExampleComponentProps> = ({ example }) => {
return <div>{example}</div>;
};
Run Code Online (Sandbox Code Playgroud)
打字稿现在告诉我:Cannot find namespace 'Content'。
我的 tsconfig 看起来像这样:
{
"compilerOptions": {
// General settings for code interpretation
"target": "esnext",
"module": "commonjs",
"jsx": "react",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"noEmit": true,
"noEmitOnError": true, …Run Code Online (Sandbox Code Playgroud) 我的应用程序根目录中有一个index.d.ts文件,其中声明了全局事物,例如全局模块。
所以我的index.d.ts看起来如下:
declare module 'moment';
Run Code Online (Sandbox Code Playgroud)
我可以导入moment任何.ts文件而不会出现任何错误。
现在,当我在index.d.ts文件中导入某些内容以进行更多声明时,就会出现问题。让我说如下:
import { MyService } from './path/to/file'; //line#1
declare module 'moment';
export interface SomeInterface {
name: string;
salary: number;
myService: MyService
}
Run Code Online (Sandbox Code Playgroud)
.ts现在我的文件中正在导入的任何导入moment都是错误的并抛出Cannot find module 'moment'.ts(2307)
我一发表评论line#1,moment导入就重新开始工作。
我在这里做错了什么?
我正在尝试从 jasmine 合并 Matchers 界面。该接口在 jasmine 命名空间内声明。
我创建了 .d.ts 文件并添加了以下内容:
declare namespace jasmine {
interface Matchers {
toBeSuccessful(): boolean;
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但如果我尝试向 .d.ts 文件添加导入语句,则它不起作用。该toBeSuccessful功能无法识别。
我还尝试在我的函数实现中添加这部分代码,而不是在 d.ts 文件中,但是,命名空间的其他成员不再可用,例如在此代码中:
class ToBeSuccessfulMatcher implements jasmine.CustomMatcher {
compare<T>(actual: Result<T>): jasmine.CustomMatcherResult {
}
}
Run Code Online (Sandbox Code Playgroud)
接口 CustomMatcher 和 CustomMatcherResult 不再可用。
我错过了什么?
我正在使用Moment.js处理TypeScript项目中的日期时间对象。我想定义一个对象类型,该对象类型的键的类型值为Moment。
但是,当我将以下内容添加到全局定义文件(test.d.ts)时,在项目中的任何位置都找不到该文件中的接口。
import { Moment } from 'moment';
interface Test {
date: Moment;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在.ts或.tsx文件中使用接口时,出现此TypeScript错误:
[at-loader] ./src/<examplefilename>.tsx:91:26
TS2304: Cannot find name 'Test'.
Run Code Online (Sandbox Code Playgroud)
VSCode的TypeScript错误检查和TSLint均未显示该代码的任何问题。
如何从外部模块导入类型以在全局定义文件中使用?
我有 React 和 TypeScript 项目。这是我在某些组件中的文件夹结构,例如:
- ComponentFolder
- index.tsx
- types.d.ts
- ChildComponent
- index.tsx
Run Code Online (Sandbox Code Playgroud)
我知道,如果我将类型放入组件目录根目录中的types.d.ts文件中,我可以在此目录中的 childComponents 中使用这些类型。
所以我尝试了;
我的types.d.ts文件:
export type CommonProps = {
openItem: string;
getValue: (e: React.MouseEvent) => void;
};
Run Code Online (Sandbox Code Playgroud)
并在子组件中:
import * as React from "react";
const ChildComponent: React.FC<CommonProps> = (props) => {
return (
<div>
{props.openItem}
</div>
);
};
export default ChildComponent;
Run Code Online (Sandbox Code Playgroud)
但出现错误:
cannot find CommonProps error.
Run Code Online (Sandbox Code Playgroud)
我如何知道目录中是否有一些文件 d.ts 我可以在该目录中使用这些类型而无需导入。
我哪里弄错了?
typescript ×8
reactjs ×3
electron ×1
express ×1
ipc ×1
javascript ×1
momentjs ×1
node.js ×1
types ×1