-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Tuple with conditional optional type #31409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
interface A<T = never> {
array: [T] extends [never] ? [] : [T]
} works |
OK, it works. There are more generic types and first one can be defined and second one omitted (and more types). But your response apply also to this. interface A<T1 = never, T2 = never> {
array: [T1] extends [never] // if T1 is not defined, then T2 is also not defined by sure
? []
: [T2] extends [never]
? [T1]
: [T1, T2]
}
let test1: A<string>;
test1.array = ["some string"]
test1.array = [] // error: correctly
let test2: A;
test2.array = [ "" ] // error: correctly
test2.array = [ undefined ] // error: correctly
test2.array = [ null ] // error: correctly
test2.array = [];
let test3: A<string, number>;
test3.array = ["some string", 55]
test3.array = [] // error: correctly This below looks better but I'm happy that above one is working interface A<T1 = never, T2 = never> {
array: [
[T1] extends [never] ? never? : T1,
[T2] extends [never] ? never? : T2
]
} Thanks a lot! |
I've encountered another problem with new solution - I need to call Simplified code: class GenericTupple<T1, T2> {
tupple: [T1] | [T1, T2];
getString() {
// This one does not work :/
// error: `Type 'string' is not assignable to type 'T1'.
return this.tupple.reduce((final, prev) => `${final}.${prev}`, '');
}
} casting helps: return (this.tupple as [T1, T2?]).reduce((final, prev) => `${final}.${prev}`, ''); This results from original code (from comment above) with conditional types (so I cannot define tupple as: class A<T1, T2 = never, T3 = never> {
array: [T2] extends [never]
? [T1]
: [T3] extends [never]
? [T1, T2]
: [T1, T2, T3]
getString(): string {
// error: `Type 'string' is not assignable to type 'T1'.`
return this.array.reduce((final, prev) => `${final}.${prev}`, '');
}
} Is it possible without casting? As I can see, incorrect Should I create new issue (this is connected a little with original question so for now I opened this one - am I missing something obvious)? |
Summary:
I can define in typescript that tuple contains optional elements, e.g.
let array: [string?];
- I can assign empty array. But can I combine this with conditional (generic) type: when type is defined then value should be set. Otherwise, value is disallowed. Or at least optional if not disallowed.TypeScript Version:
3.5.0-dev.20190515
Search Terms:
tuple with conditional optional type
Code
Expected behavior:
I can assign empty array to
test2.array
. Even more - I cannot assign not empty array.Actual behavior:
I cannot assign empty array. I cannot assign also array with some value. I have to use some other type and pass it explicitly, e.g. undefined:
Playground Link
The text was updated successfully, but these errors were encountered: