TypeScript

Beyond the basics πŸš€

Overview

  • What is the talk about? πŸ€”
  • My origin story πŸ¦Έβ€β™‚οΈ
  • What do you know now? πŸ™‹β€β™‚οΈ
  • What will you know at the end? πŸ’‘

Overview

  • What is the talk about? πŸ€”
    • Showcasing lesser known features
    • Advanced TypeScript concepts
  • My origin story πŸ¦Έβ€β™‚οΈ
  • What do you know now? πŸ™‹β€β™‚οΈ
  • What will you know at the end? πŸ’‘

Overview

  • What is the talk about? πŸ€”
  • My origin story πŸ¦Έβ€β™‚οΈ
    • Why am I giving this presentation?
  • What do you know now? πŸ™‹β€β™‚οΈ
  • What will you know at the end? πŸ’‘

Overview

  • What is the talk about? πŸ€”
  • My origin story πŸ¦Έβ€β™‚οΈ
  • What do you know now? πŸ™‹β€β™‚οΈ
    • Superset of JavaScript
    • Static type analysis
    • Extends JavaScript with new features
  • What will you know at the end? πŸ’‘

Overview

  • What is the talk about? πŸ€”
  • My origin story πŸ¦Έβ€β™‚οΈ
  • What do you know now? πŸ™‹β€β™‚οΈ
  • What will you know at the end? πŸ’‘
    • One or two new features/tricks
    • When/why to use them
    • Whether you just wasted 20 minutes πŸ˜‰

Declaration Merging

Two icecream cones merging

Declaration

"A statement which introduces a name into the program"
Chat GPT 2024
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
Why?
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
What next?
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
				
						
					
				
				
						
					
				
And the best part...
Instant legacy code! Just run tsc!
		  			
							
					  
					

πŸ™‹β€β™‚οΈ

Branded Types

A piece of paper with the word 'nice' written on it being underlined with a yellow marker
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
Did we use the right security group?

No!

(And TypeScript didn't catch our mistake 😒)

What is a branded type?

		  			
							
					  
					

What is a branded type?

		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					
		  			
							
					  
					

Success! πŸ₯³

πŸ™‹β€β™‚οΈ

any, unknown and never

Charlie from 'It's Always Sunny in Philadelphia' rubbing his temple

Foundational Concepts

  • Narrow and wide types
  • Type compatibility

Narrow vs. wide wypes

		  			
							
					  
					

Narrow vs. wide wypes

		  			
							
					  
					

Narrow vs. wide wypes

		  			
							
					  
					

Narrow vs. wide wypes

  • Any & Unknown
    • Widest possible type
    • Anything can be assigned to them
  • Never
    • Narrowest possible type
    • Nothing can be assigned to it

Type compatibility

		  			
							
					  
					

When to use "any"?

  • βœ… Non-production code (e.g. prototype or PoC)
  • βœ… Within a well defined boundary
  • βœ… When type safety is difficult or impossible
  • ❌ When you don't know the type
  • ❌ As a function parameter or return

Consider other options

		  			
							
					  
					

Consider other options

		  			
							
					  
					

When to use "unknown"?

  • βœ… When you don't know the type
  • βœ… As a function parameter or return

When to use "unknown"?

		  			
							
					  
					

When to use "unknown"?

		  			
							
					  
					

When to use "unknown"?

		  			
							
					  
					

TLDR;

Use "unknown" any time you would use "any" - unless you have a good reason not to.

When to use "never"?

  • βœ… Complex generics
  • βœ… Exhaustivness checking
  • βœ… Functions that don't return

When to use "never"?

		  			
							
					  
					

When to use "never"?

		  			
							
					  
					

When to use "never"?

		  			
							
					  
					

When to use "never"?

		  			
							
					  
					

When to use "never"?

		  			
							
					  
					

πŸ™‹β€β™‚οΈ

Conclusion

  • Declaration Merging
  • Branded Types
  • Any, Unknown and Never

Thank You! ❀️

Further Reading

QR code for the presentation
πŸ‘† Scan me to view slides!