Skip to content

Fixed typescript example#221

Closed
KendrickIW wants to merge 2 commits intoryanmcdermott:masterfrom
KendrickIW:master
Closed

Fixed typescript example#221
KendrickIW wants to merge 2 commits intoryanmcdermott:masterfrom
KendrickIW:master

Conversation

@KendrickIW
Copy link

Fixed the example to show usage of typescript. Also that same example works if you want to promote the usage of Flow

@styfle
Copy link

styfle commented Jun 15, 2017

Should you change the language of the snippet?

    ```javascript
    function wrong() { }
    ```
    ```typescript
    function fixed() { }
    ```

@ryanmcdermott
Copy link
Owner

ryanmcdermott commented Jun 15, 2017

Hey there! I've gone back and forth about this. Should this show TypeScript code at all? Since this repo is focused just on JavaScript I would prefer to keep it solely JavaScript. The subsection in question is regarding the avoidance of runtime type checks in JS, and advocates using a typed alternative. Is the advocacy enough? I welcome comments :)

@styfle
Copy link

styfle commented Jun 15, 2017

This is the description before the code snippet.

If you are working with basic primitive values like strings, integers, and arrays, and you can't use polymorphism but you still feel the need to type-check, you should consider using TypeScript. It is an excellent alternative to normal JavaScript, as it provides you with static typing on top of standard JavaScript syntax. The problem with manually type-checking normal JavaScript is that doing it well requires so much extra verbiage that the faux "type-safety" you get doesn't make up for the lost readability. Keep your JavaScript clean, write good tests, and have good code reviews. Otherwise, do all of that but with TypeScript (which, like I said, is a great alternative!).

It seems like the best way to show the "bad" and "good" is to show how to convert a run-time type check to a compile-time type check. Maybe you could add a link to the TypeScript Playground so it's a little easier to see why this is valuable.

@styfle
Copy link

styfle commented Jun 15, 2017

And now that I look at the original JS code, I think this is not easily type checked using TypeScript since the js example requires both param types to be the same.

You could add a .d.ts and overload the method with (string, string) or (number, number). Maybe we can come up with a better "bad" example that can be corrected with typescript?

@KendrickIW
Copy link
Author

To me the subsection suggested that the good was going to be an example of TypeScript. What if there was another good example that says the typescript way?

@KendrickIW
Copy link
Author

@styfle

Should you change the language of the snippet?

I wasn't aware there was support for for typescript. I can change that

@ryanmcdermott
Copy link
Owner

Every subsection should have just one Bad and one Good example.

I'm totally open to hearing a convincing argument, but as it is I'm not too sure about including another language in an example other than simply saying "go check this superset of JS if you want type-checking"

@felquis
Copy link

felquis commented Jul 16, 2017

#221 (comment) that being said, we should close this PR! Right?

@ryanmcdermott
Copy link
Owner

Looks like there's now a TypeScript clean code project, see here: https://github.com/labs42io/clean-code-typescript

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.

4 participants