undefined to the Type?https://github.com/microsoft/TypeScript/issues/36163#issuecomment-574359197
const arr = [1, 2, 3, 4, 5]
const j = Math.random() * 1000 % 1;
const k = arr[j]?.toFixed();
undefined, you'd get undefined.But we have plenty of places where we don't make || conservative.
// Inferred to `number[]` because we
// don't assume it *could* be null/undefined
function foo(x: number[]) {
return x || "hello"
}
?. for a reason, or assume we have a perfect type system??. on a defined type unless you planned to immediately handle them?|| and &&?Would be nice to write
type Foo<T> = {
[K in keyof T as StringConcat<"get-", K>]: T[K]
};
Could you do something like the following?
type Foo<T> = {
[K in StringConcat<"get-", keyof T>]: T[K]
};
Created a prototype that wires up a bunch of type aliases that have special behavior:
type StringToUpper<K> = ...; // StringToUpper<"Foo"> -> "FOO"
type StringToLower<K> = ...; // StringToLower<"Foo"> -> "foo"
// StringSlice<"hello", 1> -> "ello"
type StringSlice<K, Start extends number, End extends number> = ...;
// StringConcat<"foo", "bar"> -> "foobar"
type StringConcat<K1, K2> = ...;
// StringMatch<"foo" | "bar" | "baz", "a"> -> "bar" | "baz"
type StringMatch<K, Pattern extends string> = ...;
Won't these cause a lot of exponential explosions in the type system?
Why would you write ?. on a defined type unless you planned to immediately handle them?
People new to TS often carry the need to write defensive code _everywhere_ even if it's unnecessary. If TS implicitly added | undefined here, it would suddenly make these legitimate and make it necessary to add even _more_ defensive code to handle the new undefined values that aren't really there at runtime.
This also seems like it would break the no-unnecessary-condition lint rule, which is really useful for changing this behavior over time.
Misc. thoughts from a random user:
+ operator?$Call, $Exact, etc), so it wouldn't be too crazy IMO if TypeScript were to introduce a few$Awaited, $Coroutine, etc)We don't like the type aliases (especially the leading $), but yeah, it's the easiest way to make this "scale".
Hey @DanielRosenwasser ,
are there plans to implement the string utility types? Are you seeking contributors for those? I'd like to help, as I'm very much interested in this feature. Although I have no experience working with the TS code base at all, I'd like to invest my time. Maybe it could help me fix one bug or another in the future 😄
No plans right now - we had a mock-up demonstrated at the design meeting, but the concern was more about complexity and speed.
Most helpful comment
Hey @DanielRosenwasser ,
are there plans to implement the string utility types? Are you seeking contributors for those? I'd like to help, as I'm very much interested in this feature. Although I have no experience working with the TS code base at all, I'd like to invest my time. Maybe it could help me fix one bug or another in the future 😄