我正在尝试为第三方lib documentation制作类型文件
它的设置如下所示
index.js
var Optional = require('./lib/optional.js');
module.exports = {
empty: function empty() {
return new Optional();
},
of: function of(value) {
if (value === undefined || value === null) {
throw new Error('value is not defined');
}
return new Optional(value);
},
ofNullable: function ofNullable(value) {
return new Optional(value);
}
};
optional.js
...
...
module.exports = Optional;
我一辈子都无法弄清楚如何为此类型创建类型,除非将整个库重写为打字稿(optional-js,但我宁愿只提供适当的打字稿文件)。 / p>
我有以下
index.d.ts
import Optional from './lib/optional';
export declare function ofNullable<T>(value: T | undefined | null): Optional<T>;
export declare function of<T>(value: T | null | undefined): Optional<T>;
export declare function empty(): Optional<null>;
optional.d.ts
export default class Optional<T> {
private readonly _value;
constructor(value?: T | null | undefined);
...
...
}
当我尝试在另一个库中使用它
import Optional from 'optional-js';
...
const path: Optional<string> = Optional.ofNullable(this.props.match.params.path);
我收到以下错误
TypeScript error: Cannot use namespace 'Optional' as a type. TS2709
答案 0 :(得分:1)
如果用户无法访问Optional
的构造函数,则不要将其声明为class
(该类是您的实现详细信息)。在optional.d.ts
中:
export interface Optional<T> {
/* … only public members here … */
}
在index.d.ts
中,您可以导入和重新导出Optional
:
import { Optional } from './lib/optional';
export { Optional };
// …
或者,您可以直接在Optional
中写入和导出类型index.d.ts
。
然后,从另一个库中获取
:import { ofNullable, Optional } from 'optional-js';
// …
const path: Optional<string> = ofNullable(this.props.match.params.path);
答案 1 :(得分:0)
我能够通过将两个文件合并到一个类定义中来解决该问题。
export default class Optional<T> {
private readonly _value;
private constructor(value?: T | null | undefined);
/**
* Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
*
* @typeparam T the type of the value
* @param value the possibly-null value to describe
* @return an Optional with a present value if the specified value is non-null, otherwise an empty Optional
*/
static ofNullable<T>(value: T | undefined | null): Optional<T>;
/**
* Returns an Optional describing the given non-null value.
*
* @typeparam T the type of the value
* @param value the value to describe, which must be non-null
* @return an Optional with the value present
* @throws Error if value is null
*/
static of<T>(value: T | null | undefined): Optional<T>;
/**
* Returns an empty Optional instance. No value is present for this Optional.
*
* @return an empty Optional
*/
static empty(): Optional<null>;
/**
* If a value is present in this Optional, returns the value, otherwise throws an Error.
*
* @return the non-null value held by this Optional
* @throws Error if the value is null;
*/
get(): T | null | undefined;
/**
* Return true if there is a value present, otherwise false.
*
* @return true if there is a value present, otherwise false
*/
isPresent(): boolean;
/**
* If a value is present, invoke the specified consumer with the value, otherwise do nothing.
*
* @param consumer function to be executed if a value is present
*/
ifPresent(consumer: (value: T) => void): void;
/**
* If a value is present, and the value matches the given predicate, return an Optional describing the value,
* otherwise return an empty Optional.
*
* @param predicate A predicate to apply to the value, if present
* @return an Optional describing the value of this Optional if a value is present and the value matches the given
* predicate, otherwise an empty Optional
* @throws Error if the predicate is null
*/
filter(predicate: (value: T) => boolean): Optional<T | null | undefined>;
/**
* If a value is present, apply the provided mapping function to it, and if the result is non-null,
* return an Optional describing the result. Otherwise return an empty Optional.
*
* @typeparam U The type of the result of the mapping function
* @param mapper a mapping function to apply to the value, if present.
* @return an Optional describing the result of applying a mapping function to the value of this Optional,
* if a value is present, otherwise an empty Optional
* @throws Error if the mapping function is null
*/
map<U>(mapper: (value: T) => U | undefined | null): Optional<U>;
/**
* If a value is present, apply the provided Optional-bearing mapping function to it, return that result,
* otherwise return an empty Optional. This method is similar to map(Function), but the provided mapper is one whose
* result is already an Optional, and if invoked, flatMap does not wrap it with an additional Optional.
*
* @typeparam U The type parameter to the Optional returned by the mapping function
* @param mapper a mapping function to apply to the value, if present the mapping function
* @return the result of applying an Optional-bearing mapping function to the value of this Optional,
* if a value is present, otherwise an empty Optional
* @throws Error if the mapping function is null or returns a null result
*/
flatMap<U>(mapper: (value: T) => Optional<U> | undefined | null): Optional<U>;
/**
* If a value is present, returns the value, otherwise returns other.
*
* @param other the value to be returned, if no value is present. May be null.
* @return the value, if present, otherwise other
*/
orElse(other: T): T;
/**
* If a value is present, returns the value, otherwise returns the result produced by the supplying function.
*
* @param supplier the supplying function that produces a value to be returned
* @return the value, if present, otherwise the result produced by the supplying function
* @throws Error if no value is present and the supplying function is null
*/
orElseGet(supplier: () => T): T;
/**
* If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
*
* @param exceptionSupplier the supplying function that produces an exception to be thrown
* @return the value, if present
* @throws Error if no value is present and the exception supplying function is null
*/
orElseThrow(exceptionSupplier: () => Error): T;
/**
* If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
*
* @param action the action to be performed, if a value is present
* @param emptyAction the empty-based action to be performed, if no value is present
* @throws if a value is present and the given action is null, or no value is present and the given empty-based action is null.
*/
ifPresentOrElse(action: (value: T) => void, emptyAction: () => void): void;
/**
* If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
*
* @param optionalSupplier the supplying function that produces an Optional to be returned
* @return returns an Optional describing the value of this Optional, if a value is present,
* otherwise an Optional produced by the supplying function.
* @throws Error if the supplying function is null or produces a null result
*/
or(optionalSupplier: () => Optional<T>): Optional<T>;
}
我将构造函数标记为私有,因为您不能构造该类,因为它实际上并未导出。这是黑客吗?