Skip to content
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 LastArrayElement, Split, and Trim types #159

Merged
merged 23 commits into from Mar 21, 2021
Merged

Add LastArrayElement, Split, and Trim types #159

merged 23 commits into from Mar 21, 2021

Conversation

kainiedziela
Copy link
Contributor

@kainiedziela kainiedziela commented Nov 26, 2020

Fixes #148

Split

Represents an array of strings split using a passed-in character or character set.

Use case: defining the return type of the String.prototype.split method.

declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>;

type Item = 'foo' | 'bar' | 'baz' | 'waldo';
const items = 'foo,bar,baz,waldo';
let array: Item[];

array = split(items, ',');

TakeLast

Extracts the type of the last element of an array.

Use case: Defining the return type of functions that extract the last element of an array, for example lodash.last.

declare function lastOf<V extends any[], L extends TakeLast<V>>(array: V): L;
const array = ['foo', 2];
typeof lastOf(array); // -> number;

Trim

Removes spaces from the edges of a string.

Trim<' foo '> // => 'foo';

readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
source/take-last.d.ts Outdated Show resolved Hide resolved
source/take-last.d.ts Outdated Show resolved Hide resolved
ts41/query-result.d.ts Outdated Show resolved Hide resolved
ts41/query-result.d.ts Outdated Show resolved Hide resolved
test-d/query-result.ts Outdated Show resolved Hide resolved
@sindresorhus
Copy link
Owner

I struggle to see where I would use TakeLast in real-world code though? lodash.last would already ship the type itself, so not the best argument. Any other use-cases?

@sindresorhus
Copy link
Owner

// @mmkal In case you would be willing to help review.

Copy link
Contributor

@mmkal mmkal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see valid userland use cases for LastArrayElement, maybe getting the last argument from a function? e.g. type MyFuncOptions = LastArrayElement<Parameters<typeof myFunc>>

test-d/last-array-element.ts Outdated Show resolved Hide resolved
Co-authored-by: Misha Kaletsky <15040698+mmkal@users.noreply.github.com>
source/last-array-element.d.ts Outdated Show resolved Hide resolved
source/last-array-element.d.ts Outdated Show resolved Hide resolved
test-d/trim.ts Outdated Show resolved Hide resolved
ts41/query-result.d.ts Outdated Show resolved Hide resolved
kainiedziela and others added 4 commits November 27, 2020 18:02
Co-authored-by: Misha Kaletsky <15040698+mmkal@users.noreply.github.com>
Co-authored-by: Misha Kaletsky <15040698+mmkal@users.noreply.github.com>
Copy link
Contributor

@mmkal mmkal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to keep submitting separate reviews! Won't have much chance to look today after this.

test-d/trim.ts Show resolved Hide resolved
test-d/last-array-element.ts Outdated Show resolved Hide resolved
test-d/query-result.ts Outdated Show resolved Hide resolved
ts41/query-result.d.ts Outdated Show resolved Hide resolved
ts41/query-result.d.ts Outdated Show resolved Hide resolved
@fregante
Copy link

I don’t think QuerySelector needs to be part of type-fest. The original author of those types already published and improved on them: https://github.com/g-plane/typed-query-selector

Base automatically changed from master to main January 24, 2021 03:45
@sindresorhus
Copy link
Owner

I agree with @fregante. That package does it better than what we're doing here.

I still think the other types added here are useful though.

@kainiedziela Would you be able to remove the QueryResult type, but keep the others? And also link to https://github.com/g-plane/typed-query-selector in a Related section in the readme.

@kainiedziela kainiedziela changed the title Add QueryResult, Split, TakeLast and Trim types Add Split, TakeLast and Trim types Mar 7, 2021
@kainiedziela
Copy link
Contributor Author

There you go!

readme.md Outdated Show resolved Hide resolved
@sindresorhus sindresorhus changed the title Add Split, TakeLast and Trim types Add LastArrayElement, Split, and Trim types Mar 10, 2021
@sindresorhus sindresorhus merged commit 2f6aa0b into sindresorhus:main Mar 21, 2021
@kainiedziela kainiedziela deleted the query-selector branch March 21, 2021 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: Enhanced document.querySelector type
5 participants