volkan gul
play

Volkan Gul San Francisco Software engineer at Github Linkedin - PowerPoint PPT Presentation

Volkan Gul San Francisco Software engineer at Github Linkedin @birkasecorba @ugurvolkangul We believe in a safe and accessible economy, strengthened by institutional investment. Were working to advance participation in the digital


  1. Volkan Gul San Francisco Software engineer at Github Linkedin @birkasecorba @ugurvolkangul

  2. We believe in a safe and accessible economy, strengthened by institutional investment. We’re working to advance participation in the digital asset class. https://anchorage.com/careers/

  3. Types in Javascript: a case for TypeScript

  4. Outline What is the problem? ➔ Current status of JS and Some common issues in Javascript in real world. How can we solve it? ➔ What type of solutions do we have to fix these issues Examples & Features ➔ Example solutions to our initial problems

  5. T h e P r o b Javascript has a type l e m system* typeof x // number Quote // string At the source of every error which is // function blamed on the computer you will // undefined find at least two human errors, // boolean including the error of blaming it on the computer. // symbol // object - Tom Gibb *but, honestly, it is not the best

  6. T h e P r o b l e m Errors you’ve seen before - x is not a function - cannot read property ‘x’ of a null/undefined const x = 10; const y = x.parent; // Where error really happens return y.name; // Where error is reported

  7. T h e P r o b l e m Errors you won’t see coming function isItTooMuch( m ) { function fiveDividedBy( number ) { if (m.count > 2) { return 5 / num; return 'yes'; } } else { return 'no'; fiveDividedBy(5) // => ? } } isItTooMuch(5) // => ? Quote Only half of programming is coding. The other 90% is debugging.

  8. T h e S o l u t i o n Linters s function fiveDividedBy( number ) { return 5 / num; } fiveDividedBy(5) // => ? Added bonuses Enforce coding styles ● Cleaner code ● More optimized code ● Before runtime errors ●

  9. T h e S o l u t i o n Custom type checking s /** * @param greeting: greeting prompt * @param toWhom: list of people to greet * @param howManyTimes: how many times should function greet */ function greet( greeting , toWhom , howManyTimes ){ … } // Correct usage greet('hello there', ['General Grievous'], 1); // Incorrect usages greet(); greet('hi', 'Master Kenobi');

  10. T h e S o l u t i o n Custom type checking s function greet( greeting , toWhom , howManyTimes ) { if (arguments.length !== 3) { throw new Error('must be called with 3 arguments') Tests } Solutions like custom type checking should be paired with extensive test if (typeof greeting !== 'string') { coverage to be really effective. throw new Error('first argument must be a string') } The question you should be asking is “can somebody (or me in the future) if (!Array.isArray(toWhom)) { change and break my code without throw new Error('second argument must be an array') realizing they broke the logic?” } if (typeof howManyTimes !== 'number') { throw new Error('third argument must be a number') } }

  11. T h e S o l u t i o n React PropTypes s import PropTypes from 'prop-types'; class Greeting extends React.Component { render() { return ( <h1>Hello, {this.props.name}</h1> ); } } Greeting.propTypes = { name: PropTypes.string };

  12. T h e S o l u t i o n Static type checking s What is Typescript It is a strict syntactical superset of JavaScript, and adds optional static typing to the language.

  13. T h e S o l u t i o n Static type checking s JS -> TS

  14. E x a m p l e s Static type checking

  15. E x a m p l e s Static type checking

  16. E x a m p l e s Static type checking

  17. E x a m p l e s Static type checking Typeahead for argument types and return type as you write ● the function

  18. E x a m p l e s Static type checking Function description and JSdocs documentation on hover ●

  19. E x a m p l e s Static type checking Meaningful errors pre-runtime ●

  20. E x a m p l e s Static type checking Optional parameters and default values ●

  21. Conclusion Javascript doesn’t give us enough type errors ● It is time consuming and difficult to manage custom type error handling ● It is tedious to find hidden bugs caused by type mismatch ● Tools like linters and static type checkers helps us to maintain , document ● and optimize our code with almost no overhead.

  22. References & Docs Typescript Docs https://www.typescriptlang.org/ Type Systems Will Make You a Better JavaScript Developer - React Conf 2017 Jared Forsyth https://www.youtube.com/watch?v=V1po0BT7kac Why I Was Wrong About TypeScript - GOTO 2018 TJ VanToll https://www.youtube.com/watch?v=AQOEZVG2WY0 Create React App Docs https://create-react-app.dev/docs/adding-typescript/

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend