Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 1.66 KB

java-and-csharp.md

File metadata and controls

61 lines (45 loc) · 1.66 KB

Are you a Java or C# dev trying to learn TypeScript?

If you're coming from Java or C#, this is a great article for you to understand some differences between your ideas of typed languages vs TypeScript - https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-oop.html

In general, it can be easier to think of TS as a "linter" more so than a programming language. Ultimately, your types will not end up in production code. TS will "compile" to JS which in this case mostly just means that the types are stripped out so the end result is valid JS.

Also here are some high level points:

type User = {
  id: number
  name: string
}

function getName(user: User): string {
  return user.name
}

// Name is inferred to be a string
const name = getName({ id: 1, name: 'Brad' })
  • In TS, unions can be value-specific:
// Variables of Animal type can only be these specific strings
// Essentially this creates an enum:
type Animal = 'dog' | 'cat'
  • In TS, if a variable can be null or undefined, it needs to be explicitly typed:
// variables of type User cannot be null or undefined
type User = {
  id: number
  name: string
}

function getName(user: User | null): string | null {
  return user && user.name
}

// Fails. undefined is not the same as null and the function only wants
// User | null
getName(undefined)

// Fails. The getName function could take null, but `myself` is a User type
// that cannot be null
const myself: User = null
getName(myself)
  • TS has an any type
let x: any = 10
x = { age: 50 } // Valid