TS Confuses namespace with the class on JSDoc module.exported class with static method.
@sandersn đang làm issue này rồi.
Từ ngày 4/10/2023.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
🔎 Search Terms
"module.exports static method" "namespace vs class confusion" "namespace vs class collision" "Two different types with this name exist, but they are unrelated."
🕗 Version & Regression Information
Versions tried:
- Version 5.3.0-dev (55d8bed85c11672b2b6f9ee3e6e1d384bef6f4da - )
- Version 5.2.2
- Version 5.1.6
- Version 5.0.4
- Version 4.9.5
⏯ Playground Link
💻 Code
// @filename: name.js
'use strict';
/**
* Options for the netaddress.
* @typedef {Object} NameOptions
* @property {String} name
*/
/**
* Net Address
* Represents a network address.
*/
class Name {
/** @type {String} */
name;
/**
* Create a network address.
*/
constructor() {
this.name = 'test';
}
/**
* Inject properties from host, port, and network.
* @param {String} name
* @returns {this}
*/
fromName(name) {
this.name = name;
return this;
}
/**
* @param {String} name
* @returns {Name}
*/
static fromName(name) {
return new this().fromName(name);
}
}
/*
* Expose
*/
module.exports = Name;
// @filename: usage.js
'use strict';
const Name = require('./name');
/** @type {Name} */
let address;
address = new Name();
address.name = 'somename';
🙁 Actual behavior
This reports an error:
Type 'import("/name")' is not assignable to type 'import("/name")'. Two different types with this name exist, but they are unrelated.
The types returned by 'fromName(...)' are incompatible between these types.
Type 'Name' is not assignable to type 'this'.
'Name' is assignable to the constraint of type 'this', but 'this' could be instantiated with a different subtype of constraint 'Name'.
🙂 Expected behavior
new Name() and @type {Name} should be the same as module.exports = Name class.
Additional information about the issue
Repository format: https://github.com/nodech/type-tsjs-tests
generated declaration file for name.js is in the form:
export = Name;
declare class Name {...}
declare namespace Name { export { NameOptions } }
type NameOptions
If you remove @typedef {Object} NameOptions from the name.js, the declaration becomes
export = Name;
declare class Name {...`
and issue goes away.
More confusing
If you leave NameOptions but remove static member static fromName of the class Name, it still has namespace and class with the same name, BUT issue goes away ?
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 15 giờ
- Pull request đã merge (30 ngày)
- 106
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/TypeScript
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64322 · 2 bình luận · 1 reaction · 2 người được giao ·
-
Possible Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
microsoft/TypeScript#64278 · 1 bình luận · 1 reaction ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
microsoft/TypeScript#64118 · 1 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64094 ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
microsoft/TypeScript#63959 · 5 bình luận ·
Tất cả issue của microsoft/TypeScript
Issue tương tự
-
Type/Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
OpenNSW/nsw-srilanka#497 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
kubernetes-sigs/prow#953 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
caddyserver/caddy#8046 ·