脚.ts的内容:
let a: Person = new Person();
Run Code Online (Sandbox Code Playgroud)
bar.ts 的内容:
class Person {}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json 文件包含由tsc --init
.
我正在使用打字稿版本 2.6.2。
上面的代码简单地编译,没有关于Person
未定义的错误。如果我在 foo.ts 中重命名Person
为Persons
,那么它确实会抛出一个错误,说cannot find name 'Persons'
. 所以就好像它以某种方式自动导入这个文件。
另请注意,我没有Person
在 bar.ts 中导出该类。如果我确实添加了导出语句,那么一切都会按照它应该的方式运行 - 我收到一条错误消息,说cannot find name 'Person'
.
有谁知道这里发生了什么?这是用户错误吗?
编辑:
也许这在浏览器中有意义,但对我来说这种行为在 node.js 应用程序中没有意义。是否有禁止这种行为的 Typescript 标志?
以下代码有2个定义operator+
- 一个在类上Foo
,另一个是独立函数.
我觉得编译器应该抱怨这个,但事实并非如此.当我operator+
在main函数中使用时,它会选择类中定义的那个.当我删除类中的那个时,它开始使用独立功能.
删除类方法以静默方式更改C++程序的行为这一事实非常令人担忧.这有什么理由吗?
#include <iostream>
class Foo
{
public:
int operator+(const Foo& b)
{
return 5;
}
};
int operator+(const Foo& a, const Foo& b)
{
return 6;
}
int main()
{
Foo a, b;
int c{ a + b };
std::wcout << c << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我知道可以用C编程语言将SIGTERM
,SIGINT
等等发送到您自己的进程中:
https://www.gnu.org/software/libc/manual/html_node/Signaling-Yourself.html
Node.js是否提供此功能?