-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add isFulfilled and isRejected for PromiseSettledResult[] #30
Comments
I don't intent to modify built-in methods here. We could make a similar method to |
@sindresorhus What about adding a method function hasOwn<Key extends PropertyKey>(key: Key) {
return function <ObjectType>(object: ObjectType): object is Extract<ObjectType, Record<Key, unknown>> {
return objectHasOwn(object, key);
}
}
const results: PromiseSettledResult<string>[] = [{ status: 'fulfilled', value: 'foo' }]
const r = results.filter(hasOwn('value')); |
We have such method already. |
Do you mean const has = Object.prototype.hasOwnProperty;
function objectHasOwn<ObjectType, Key extends PropertyKey>(
object: ObjectType,
key: Key,
): object is ObjectType & Record<Key, unknown> {
// TODO: Use `Object.hasOwn()` when targeting Node.js 16.
return has.call(object, key);
}
const results: PromiseSettledResult<string>[] = [{ status: 'fulfilled', value: 'foo' }, { status: 'rejected', reason: 'foo' }]
// typeof fulfilled is still PromiseSettledResult not PromiseFulfilledResult
const fulfilled = results.filter(r => objectHasOwn(r, 'value'));
function isFulfilled<T>(object: PromiseSettledResult<T>) {
return objectHasOwn(object, 'value');
}
// typeof fulfilled2 is still PromiseSettledResult not PromiseFulfilledResult
const fulfilled2 = results.filter(isFulfilled); The goal of |
That's not really an issue with |
Thank you for your replies! @jonahsnider @sindresorhus I think https://github.com/robertmassaioli/ts-is-present is exactly what I want. |
No description provided.
The text was updated successfully, but these errors were encountered: