안전한 탐색 연산자 (?.) 또는 (!.) 및 null 속성 경로를 입력합니다.
Angular2 템플릿에는 안전한 연산자 (?.)가 있지만 component.ts (typescript 2.0)에는 있습니다. 안전한 탐색 연산자 (!.)가 작동하지 않습니다.
예:
이 TypeScript
if (a!.b!.c) { }
이 JavaScript로 컴파일됩니다.
if (a.b.c) { }
하지만 실행하면 다음 오류가 발생합니다.
정의되지 않은 속성 'b'를 읽을 수 없습니다.
다음에 대한 대안이 있습니까?
if (a && a.b && a.b.c) { }
?
최신 정보:
선택적 체이닝 / 안전한 탐색 제안은 3 단계에 있으며 곧 Typescript에 적용될 것입니다.
현재 typescript에는 안전한 탐색 연산자가 없습니다 (아직 논의 중입니다 ).
!.
null이 아닌 주장 연산자 - 그것은 단지 검사기를 입력 할 말을 당신이있어 그 확실 a
하지 않다 null
나 undefined
.
여기에 더 많은 정보
최신 정보:
3.7 릴리스 https://github.com/microsoft/TypeScript/issues/33352 의 범위에서 계획 됨
이와 같은 사용자 지정 함수를 작성해 볼 수 있습니다.
이 접근 방식의 주요 장점은 유형 검사와 부분적인 지능입니다.
export function nullSafe<T,
K0 extends keyof T,
K1 extends keyof T[K0],
K2 extends keyof T[K0][K1],
K3 extends keyof T[K0][K1][K2],
K4 extends keyof T[K0][K1][K2][K3],
K5 extends keyof T[K0][K1][K2][K3][K4]>
(obj: T, k0: K0, k1?: K1, k2?: K2, k3?: K3, k4?: K4, k5?: K5) {
let result: any = obj;
const keysCount = arguments.length - 1;
for (var i = 1; i <= keysCount; i++) {
if (result === null || result === undefined) return result;
result = result[arguments[i]];
}
return result;
}
및 사용법 (최대 5 개의 매개 변수 지원 및 확장 가능) :
nullSafe(a, 'b', 'c');
놀이터의 예 .
외부 라이브러리를 사용하는 또 다른 대안은 _.has () 에서 Lodash .
예
_.has(a, 'b.c')
와 동등하다
(a && a.b && a.b.c)
편집 : 주석에서 언급 했듯이이 방법을 사용할 때 Typescript의 유형 추론을 잃습니다. 예를 들어, 개체가 올바르게 입력되었다고 가정하면 z가 개체 b의 필드로 정의되지 않으면 (a && ab && abz)와 함께 컴파일 오류가 발생합니다. 그러나 _.has (a, 'b.z')를 사용하면 해당 오류가 발생하지 않습니다.
ts-optchain 이라는 새 라이브러리 는이 기능을 제공하며 lodash의 솔루션과 달리 유형을 안전하게 유지합니다. 다음은 사용 방법에 대한 샘플입니다 (readme에서 가져옴).
import { oc } from 'ts-optchain';
interface I {
a?: string;
b?: {
d?: string;
};
c?: Array<{
u?: {
v?: number;
};
}>;
e?: {
f?: string;
g?: () => string;
};
}
const x: I = {
a: 'hello',
b: {
d: 'world',
},
c: [{ u: { v: -100 } }, { u: { v: 200 } }, {}, { u: { v: -300 } }],
};
// Here are a few examples of deep object traversal using (a) optional chaining vs
// (b) logic expressions. Each of the following pairs are equivalent in
// result. Note how the benefits of optional chaining accrue with
// the depth and complexity of the traversal.
oc(x).a(); // 'hello'
x.a;
oc(x).b.d(); // 'world'
x.b && x.b.d;
oc(x).c[0].u.v(); // -100
x.c && x.c[0] && x.c[0].u && x.c[0].u.v;
oc(x).c[100].u.v(); // undefined
x.c && x.c[100] && x.c[100].u && x.c[100].u.v;
oc(x).c[100].u.v(1234); // 1234
(x.c && x.c[100] && x.c[100].u && x.c[100].u.v) || 1234;
oc(x).e.f(); // undefined
x.e && x.e.f;
oc(x).e.f('optional default value'); // 'optional default value'
(x.e && x.e.f) || 'optional default value';
// NOTE: working with function value types can be risky. Additional run-time
// checks to verify that object types are functions before invocation are advised!
oc(x).e.g(() => 'Yo Yo')(); // 'Yo Yo'
((x.e && x.e.g) || (() => 'Yo Yo'))();
@Pvl의 대답을 바탕으로 재정의를 사용하는 경우 반환 값에 형식 안전성을 포함 할 수 있습니다.
function dig<
T,
K1 extends keyof T
>(obj: T, key1: K1): T[K1];
function dig<
T,
K1 extends keyof T,
K2 extends keyof T[K1]
>(obj: T, key1: K1, key2: K2): T[K1][K2];
function dig<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2]
>(obj: T, key1: K1, key2: K2, key3: K3): T[K1][K2][K3];
function dig<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3]
>(obj: T, key1: K1, key2: K2, key3: K3, key4: K4): T[K1][K2][K3][K4];
function dig<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4]
>(obj: T, key1: K1, key2: K2, key3: K3, key4: K4, key5: K5): T[K1][K2][K3][K4][K5];
function dig<
T,
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
K4 extends keyof T[K1][K2][K3],
K5 extends keyof T[K1][K2][K3][K4]
>(obj: T, key1: K1, key2?: K2, key3?: K3, key4?: K4, key5?: K5):
T[K1] |
T[K1][K2] |
T[K1][K2][K3] |
T[K1][K2][K3][K4] |
T[K1][K2][K3][K4][K5] {
let value: any = obj && obj[key1];
if (key2) {
value = value && value[key2];
}
if (key3) {
value = value && value[key3];
}
if (key4) {
value = value && value[key4];
}
if (key5) {
value = value && value[key5];
}
return value;
}
놀이터의 예 .
'development' 카테고리의 다른 글
SpringData의 MongoTemplate과 MongoRepository의 차이점은 무엇입니까? (0) | 2020.10.14 |
---|---|
Visual Studio 2015에서 NPM 패키지 복원을 비활성화하려면 어떻게해야합니까? (0) | 2020.10.14 |
한 저장소에서 다른 저장소로 git 패치를 적용하는 방법은 무엇입니까? (0) | 2020.10.14 |
메모리 할당 : 스택 대 힙? (0) | 2020.10.14 |
영숫자, 대시 및 밑줄 (공백 없음) 정규식 확인 JavaScript (0) | 2020.10.14 |