function Name<S extends string | undefined>(name: S): S {
return name;
}
But this doesn't work:
function Name<S extends string | undefined>(name?: S): S {
return name;
// ~~~~ Type 'T | undefined' is not assignable to type 'T'
}
Name(); // type is string | undefined
const x = {
foo(a: string) => string,
foo(a: number) => string,
foo(a: string | number) => a {
return a;
}
};
class X {
// this can be auto-inferred
foo() {
return 1;
}
// this can't
get foo() {
return 1;
}
}
type X = import|
// ^ if I type `(` here, it completes to `ImportAssertions`
This is because (
is a commit character, and "accept suggestions on commit characters" is on by default.
const [a]: [string] = "".split(" ");
// ~~~ Type 'string[]' is not assignable to type '[string]'.
// Target requires 1 element(s) but source may have fewer
declare const x: Record<string, string>;
if ("a" in x) {
const y: string = x["a"];
// ~ Type 'string | undefined' is not assignable to type 'string'.
}
I have exactOptionalPropertyTypes
and noUncheckedIndexedAccess
on. I can see why this is
happening, but with exactOptionalPropertyTypes
on, I would expect the type of x["a"]
to be
string
after the in
check.
interface Hello {
hello(): string;
}
class X implements Hello {
override hello() {
// ~~~~~ This member cannot have an 'override' modifier because
// its containing class 'X' does not extend another class
return "hello";
}
}
What about this?
interface Hello {
hello(): string;
}
class X implements Hello {
implements hello() {
return "hello";
}
}