A = { name: string } type B = { name: string } type C = { address: string } const b = { name: 'B' } as const satisfies B const c = { address: 'kyoto' } as const satisfies C const a: A = b // OK const a2: A = c // 型チェックエラー // error: TS2741 [ERROR]: Property 'name' is missing in type '{ readonly address: "kyoto"; }' but required in type 'A'.
scala.reflect.Selectable.reflectiveSelectable def printName(obj: { def name: String }): Unit = { println(obj.name) } case class Person(name: String) printName(Person("Alice")) // OK
class Animal { #name: string constructor(name: string) { this.#name = name } get name() { return this.#name } } class Robot { readonly #name: string constructor(name: string) { this.#name = name } get name() { return this.#name } } function serialize(a: Animal) { return new URLSearchParams(Object.entries(a)).toString() } // error: TS2345 [ERROR]: Argument of type 'Robot' is not assignable to parameter of type 'Animal'. // Property '#name' in type 'Robot' refers to a different member that cannot be accessed from within type 'Animal'. console.log(serialize(new Robot('name')))
unique symbol type Brand<T, B> = T & { [_brand]: B } type Name = Brand<string, "name"> type Address = Brand<string, "address"> const name = "TSKaigi Kansai" as Name const address: Address = name // error: TS2322 [ERROR]: Type 'Name' is not assignable to type 'Address'. // Type 'Name' is not assignable to type '{ [_brand]: "address"; }'. // Types of property '[_brand]' are incompatible. // Type '"name"' is not assignable to type '"address"'.