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.

A thing or two about type safety

See original GitHub issue

Hey, so on the discord server @postspectacular told me about the typesafe versions of paths. I took a look and I was not so happy to see this giant mess (it’s actually a lot more but if I make the text even smaller it becomes hard to read): this

Based on the fact those things are imported from @thi.ng/api I’m pretty sure other packages contain code like this as well… But we can do more! Typescript has a pretty neat type system which allows us to write stuff like that without all the repetition!

Final result:

Before going into more details, here’s the final result of the refactor for the code above: refactored code

And the good thing is, this works for any path length!

Building blocks:

To be able to achive that I had to write a few basic types:

Note: In case you want to see the actual implementations of the types you can take a look at this typescript playground link

  • Head: returns the first element of a tuple:
type HeadTest1 = Head<[1, 2, 3]>; // 1
type HeadTest2 = Head<[]>; // never
type HeadTest3 = Head<"not an array">; // never
  • Tail: returns everything except the first element of a tuple:
type TailTest1 = Tail<[1, 2, 3]>; // [2, 3]
type TailTest2 = Tail<[]>; // []
type TailTest3 = Tail<"not an array">; // never
  • Concat: Tried adding an item at the start of a tuple:
type ConcatTest1 = Concat<0, [1, 2, 3]>; // [0, 1, 2, 3]
type ConcatTest2 = Concat<0, "not an array">; // never
  • Reverse: pretty explanatory name, reverses the order of elements of a tuple:
type ReverseTest1 = Reverse<[1, 2, 3]>; // [3,2,1]
type ReverseTest2 = Reverse<[]> // []
type ReverseTest3 = Reverse<"not an array"> // never

Those are the building blocks for a lot of more complex types. Question 1: where should I add these? (I’m thinking of @thi.ng/api but I’m not sure

The actual path stuff

Appying the same logic I was able to build a path validator and a path applier which work with any amount of nesting:

interface Nested {
    a: {
        b: {
            c: number;
        };
    };
}

type Test1 = ValidatePath<Nested, ["a", "b"]>; // ["a","b"]
type Test2 = ValidatePath<Nested, ["a", "d"]>; // never

type Test3 = RetrivePath<Nested, Test1>; // { c: number }
type Test4 = RetrivePath<Nested, Test2>; // never

Question 2: Does this seem interesting? Should I open a PR making the entire lib use it?

Note: The paths package was just an example, this could be applied to a lot of other stuff!!!

So, let’s discuss it! What do yall think of this method of typing? Should I open PRs with it?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
vycdevcommented, Jan 5, 2020

Great idea

0reactions
Mateiadrielrafaelcommented, Jan 29, 2020

I think we can close this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type safety
In computer science, type safety and type soundness are the extent to which a programming language discourages or prevents type errors. Type safety...
Read more >
What Is Type Safety - Level Up Coding - gitconnected
In summary, type safety is that compilers give an error when compiling the code and do not compile the code if there is...
Read more >
PHP or Type Safety: Pick any two
Type safety measures the ability of available language tooling to help avoid type errors when running code in a production environment. What's a ......
Read more >
What is type safety? - The PL Enthusiast
Type safety is the property of a programming language, ensuring its programs are well defined and creating a foundation for reasoning.
Read more >
What is type safety?. Often, we hear that writing softwares…
Often, we hear that writing softwares using “statically typed languages” makes them type safe. Some even claim that “type safety means ...
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