question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce Extends

See original GitHub issue

Hi Joseph,

there is the use-case of testing if only certain TS types cover a special type definition.

// a type that reflects functions and arrow functions
type Fn<A extends any[] = any[], R = any> = (...args: A) => R;

I need to ensure that only and only function cover the Fn type. E.g. type Fn = any would cover functions but also other types that are not intended to be expected. The existing type Equals is unsuitable for testing if types are inside or outside of a domain (read: a set of valid types).

Introducing an Extends type to tsafe would make it possible to assert that Fn covers the types we intend to cover and to ensure that other types are not covered (as expected).

👇👇👇

type Extends<T1, T2> = T1 extends T2 ? true : false;

By having the following unit tests, we would ensure that the Fn type does not cover more types than expected.

describe('type Fn', () => {

    function fn() {}
    
    class A {}

    // "sunny path": these would sill succeed if `type Fn = any`
    it('should cover () => any', () => { tsafeAssert<Extends<() => any, Fn>>(); });
    it('should cover () => void', () => { tsafeAssert<Extends<() => void, Fn>>(); });
    it('should cover (...args: any[]) => any', () => { tsafeAssert<Extends<(...args: any[]) => any, Fn>>(); });
    it('should cover typeof fn', () => { tsafeAssert<Extends<typeof fn, Fn>>(); });

    // "correctness": these would fail if `type Fn = any`
    it('should not cover undefined', () => { tsafeAssert<Not<Extends<undefined, Fn>>>(); });
    it('should not cover null', () => { tsafeAssert<Not<Extends<null, Fn>>>(); });
    it('should not cover boolean', () => { tsafeAssert<Not<Extends<boolean, Fn>>>(); });
    it('should not cover number', () => { tsafeAssert<Not<Extends<number, Fn>>>(); });
    it('should not cover string', () => { tsafeAssert<Not<Extends<string, Fn>>>(); });
    it('should not cover array', () => { tsafeAssert<Not<Extends<any[], Fn>>>(); });
    it('should not cover object', () => { tsafeAssert<Not<Extends<object, Fn>>>(); });
    it('should not cover class', () => { tsafeAssert<Not<Extends<A, Fn>>>(); });

});

If you also think that Extends would be a great fit for tsafe, I would be happy to create a PR!

What do you think?

Thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
danieldietrichcommented, Oct 2, 2022

Verny nice! Thank you 😃

1reaction
garronejcommented, Oct 2, 2022

BTW, i’m using extends already, in https://github.com/codegouvfr/react-dsfr

Read more comments on GitHub >

github_iconTop Results From Across the Web

extends - JavaScript - MDN Web Docs - Mozilla
The extends keyword is used in class declarations or class expressions to create a class that is a child of another class.
Read more >
Extended Essay Guide: The Introduction - LibGuides
The goal of the introduction is to introduce the topic and provide enough information about it in order to enable the reader to...
Read more >
Extended Self-Introduction - SpanishPod101
Extended Self-Introduction. Go beyond the self-introduction with the first of our core learning paths! 32 Lessons • 3hrs 6min. Add to Dashboard.
Read more >
Connect Extend Challenge - THINKING PATHWAYS
​The Connect Extend Challenge routine helps learners to: make connections between new ideas and prior knowledge; recognise ongoing questions, puzzles and ...
Read more >
Introduction to extending services—ArcGIS Server
You can extend ArcGIS Server map and image services with custom logic that can be executed in ArcGIS clients. There are two ways...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found