type-narrowing Questions
0
C++17 allows scoped enums to be initialized with integers as long as it's not narrowing, e.g.
#include <cstdint>
enum class e16 : uint16_t { x16, y16 };
enum class e32 : uint32_t { x32, y32 }...
Andria asked 23/10 at 23:35
5
Solved
Why does TypeScript not apply type narrowing on members of objects to the object type itself, such that it can be passed to another function that expects a narrowed type? How can this be fixed/circ...
Everett asked 13/9, 2019 at 18:49
2
Solved
Consider I have a python class that has a attributes (i.e. a dataclass, pydantic, attrs, django model, ...) that consist of a union, i.e. None and and a state.
Now I have a complex checking functio...
Herbertherbicide asked 30/11, 2022 at 8:11
3
Solved
Consider this simple snippet. I'm also pasting it here:
type A =
| {
b: number;
}
| {
c: number;
};
function f1(a: A) {
if ('b' in a) {
return a['b']; // No problem!
}
return 42;
}
func...
Vowel asked 30/10, 2020 at 23:34
1
Solved
Typescript refuses to narrow with a call to fail but will narrow with a call to fail2. Is this a bug in typescript?
const fail = (message?: string): never => {
throw new Error(message);
};
fun...
Coverage asked 26/2, 2023 at 17:32
3
Solved
Why do tests 1 and 2 work here, but test 3 shows a compiler error at foo[barConst]++: 'Object is possibly "undefined".'? I often need to access properties via bracket notation and thus li...
Exchequer asked 11/9, 2019 at 11:24
1
Solved
In typescript:
let str: string = 'abc';
let val: unknown = 'test';
if (typeof val === 'string') {
str = val;
}
// this code does not report any error, everything works fine.
but, if I change th...
Ramiah asked 15/11, 2021 at 23:23
2
Solved
I would like to link 2 generics types in a function, and use narrowing for both types by checking one of them. What is the correct way to do this?
type A = 'A';
type B = 'B';
type AB = A | B
type...
Sucrose asked 23/8, 2021 at 18:11
3
Solved
Why do I get a Cannot invoke an object which is possibly 'undefined' Typescript error even after I check that the func reference is not undefined?
type Hoge = {
func?: (str: string) => boolean
...
Thermy asked 14/7, 2021 at 3:56
3
Solved
Note: TypeScript is invoked with tsc --strict for all code shown below.
Given a singleton object o:
const o = {
foo: 1,
bar: 2,
baz: 3,
};
If I have a string value (say from user input) that ca...
Gambia asked 15/5, 2021 at 23:19
1
VS Code Version:
I'm trying some 'Type Narrowing' Code in VS Code, but VS Code gives a different type of information than TypeScript Playground:
VS Code gives:
the return type of Document.getEleme...
Epiphytotic asked 24/10, 2020 at 10:13
2
Solved
function isFish(pet: Fish | Bird): pet is Fish {
return (<Fish>pet).swim !== undefined;
}
tells typescript that pet type is Fish
Is there a way to state the contrary, that the input param...
Rictus asked 16/6, 2018 at 20:37
1
Solved
I have a problem when I try to use instanceof with derived class instances in a if-else statement. Consider the following example:
interface IBaseModel {
id: string
}
class BaseClass {
model: I...
Vaginectomy asked 28/7, 2017 at 19:33
1
Solved
I read about narrowing conversion on the cpp reference website. I kind of understood it but what i am not getting is that why is the error present only in the first line.
long double ld = 3.14159...
Patellate asked 17/5, 2017 at 17:44
1
Solved
The following line of code is accepted by the compiler (sun-jdk-8u51) without any warnings or errors:
short b = true ? 1 : 1;
Whereas the next two code lines lead to a compilation error (incompa...
Bridie asked 11/11, 2015 at 12:18
1
© 2022 - 2024 — McMap. All rights reserved.