A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact.
!
์ฐ์ฐ์(๋จ์ธ ์ฐ์ฐ์)๋ ํผ์ฐ์ฐ์๊ฐ null
๋๋ undefined
๊ฐ ์๋์ ๋จ์ธํ ๋ ์ฌ์ฉ// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
// Throw exception if e is null or invalid entity
// e๊ฐ null ์ด๊ฑฐ๋ ์ ํจํ์ง ์์ entity์ผ ๊ฒฝ์ฐ ์์ธ ๋ฐ์
}
function processEntity(e?: Entity) {
validateEntity(e);
// Assert that e is non-null and access name
// e๋ null์ด ์๋์ ๋จ์ธํ์ฌ name์ ์ ๊ทผํ ์ ์๋ค.
let s = e!.name;
}
interface Foo {
bar?: string;
}
const foo: Foo = getFoo();
// Incorrect code for this rule
const includesBaz: boolean = foo.bar!.includes('baz');
// Correct code for this rule
const includesBaz: boolean = foo.bar && foo.bar.includes('baz');
์ฐธ๊ณ