{"id":463,"hash":"ada55bb85c778802104fad7fba28dade4fb1df78d720d0acfc6440af9d071f1a","pattern":"TypeScript: Type&#160;&#39;string&#160;|&#160;undefined&#39;&#160;is&#160;not&#160;assignable&#160;to&#160;type&#160;&#39;string&#39;","full_message":"When I make any property of an interface optional, and while assigning its member to some other variable like this:\n\ninterface Person {\n  name?: string,\n  age?: string,\n  gender?: string,\n  occupation?: string,\n}\n\nfunction getPerson() {\n  let person = <Person>{name:\"John\"};\n  return person;\n}\n\nlet person: Person = getPerson();\nlet name1: string = person.name; // <<< Error here\n\nI get an error like the following:\n\nTS2322: Type 'string | undefined' is not assignable to type 'string'.\nType 'undefined' is not assignable to type 'string'.\n\nHow do I get around this error?","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"You can now use the non-null assertion operator that is here exactly for your use case.\n\nIt tells TypeScript that even though something looks like it could be null, it can trust you that it's not:\n\nlet name1:string = person.name!; \n//                            ^ note the exclamation mark here","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/54496398/typescript-type-string-undefined-is-not-assignable-to-type-string","votes":635,"created_at":"2026-04-19T04:51:11.498458+00:00","updated_at":"2026-04-19T04:51:11.498458+00:00"}