Код: Выделить всё
class Component { /* ... */ }
class MyComponent extends Component { /* ... */ }
function findComponent(cls) {
// searches an array of components and returns the first component that is
// instanceof cls, or null if not found
}
let comp = findComponent(MyComponent);
// comp is of type MyComponent or null
Код: Выделить всё
let foo = findComponent(FooComponent); // VS Code knows foo is type {FooComponent|null}
let bar = findComponent(BarComponent); // VS code knows bar is type {BarComponent|null}
< /code>
Наиболее очевидным решением для меня было бы что-то вроде этого: < /p>
/**
* @template {Component} C
* @param {typeof C} cls
* @returns {?C}
*/
function findComponent(cls) {}
< /code>
Но "c" в typeof c < /code> подчеркнута с ошибкой < /p>
'C' only refers to a type, but is being used as a value here.ts(2693)
Я также попробовал @param {c.constructor} cls Я видел где -нибудь, но это жаловалось
Код: Выделить всё
Cannot access 'C.constructor' because 'C' is a type, but not a namespace. Did you mean to retrieve the type of the property 'constructor' in 'C' with 'C["constructor"]'?ts(2713).< /code> < /p>
< /blockquote>
«имело большое значение», предложение приводит к другой ошибке, < /p>
Type '"constructor"' cannot be used to index type 'C'.ts(2536)
Как проверка здравомыслия о моем понимании типа модификатора, я попробовал это, и он работает, как и ожидалось, с конкретным типом, но возвращение не специфично, поэтому я не совсем нацелен.
Код: Выделить всё
/**
* @param {typeof Component} cls
* @returns {?Component}
*/
function findComponent(cls) {}
let foo = findComponent(MyComponent);
// foo is of type Component or null
Подробнее здесь: https://stackoverflow.com/questions/797 ... ameter-and