feat(question): add #15260 - Tree path array (#15261)

Co-authored-by: Neil Richter <19751938+noook@users.noreply.github.com>
This commit is contained in:
github-actions[bot] 2022-10-27 15:17:45 +08:00 committed by GitHub
parent a57e8323c2
commit 13102e9baf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,28 @@
Create a type `Path` that represents validates a possible path of a tree under the form of an array.
Related challenges:
- [Object key path](https://github.com/type-challenges/type-challenges/blob/main/questions/07258-hard-object-key-paths/README.md)
```ts
declare const example: {
foo: {
bar: {
a: string;
};
baz: {
b: number
c: number
}
};
}
/* Possible solutions:
[]
['foo']
['foo', 'bar']
['foo', 'bar', 'a']
['foo', 'baz']
['foo', 'baz', 'b']
['foo', 'baz', 'c']
*/
```

View File

@ -0,0 +1,6 @@
difficulty: hard
title: Tree path array
author:
github: noook
name: Neil Richter

View File

@ -0,0 +1 @@
type Path<T> = any

View File

@ -0,0 +1,20 @@
import type { ExpectTrue,ExpectFalse,ExpectExtends } from '@type-challenges/utils'
declare const example: {
foo: {
bar: {
a: string;
};
baz: {
b: number
c: number
}
};
}
type cases = [
ExpectTrue<ExpectExtends<Path<typeof example['foo']['bar']>, ['a']>>,
ExpectTrue<ExpectExtends<Path<typeof example['foo']['baz']>,['b'] | ['c'] >>,
ExpectTrue<ExpectExtends<Path<typeof example['foo']>, ['bar'] | ['baz'] | ['bar', 'a'] | ['baz', 'b'] | ['baz', 'c']>>,
ExpectFalse<ExpectExtends<Path<typeof example['foo']['bar']>, ['z']>>,
]