-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Fix AvailableA PR has been opened for this issueA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Description
TypeScript Version: >=3.9.2
Search Terms:
initialization type check, conditional type
Code
/**
* Type that allows to create a Tree with different shape and controllable level of deepness
* */
type Length<T extends any[]> = T['length']
type Prepend<V, T extends any[]> = ((head: V, ...args: T) => void) extends ((...args: infer R) => void)
? R
: any;
/* BuildTree allow to create tree with predefined level of nesting */
type BuildTree<T, N extends number = -1, I extends any[] = []> = {
1: T
0: (T & { children: (BuildTree<T, N, Prepend<any, I>>)[] })
} [ Length<I> extends N ? 1 : 0]
interface User {
name: string;
}
type GrandUser = BuildTree<User, 2>;
const grandUser: GrandUser = {
name: 'Grand User',
children: [{
name: 'Son',
children: [{
name: 'Grandchild',
children: [{
name: '123',
children: [{
name: 'Some other name'
}]
}]
}]
}]
};
grandUser.children[0].children[0].children[0]
Expected behavior:
Compiler should complaint about children field in 'Grandchild' during grandUser
variable initialization.
Actual behavior:
Compiler works correctly for the next line grandUser.children[0].children[0].children[0]
, but not for initialization.
Looks like regression, because in 3.8.3 it works correctly
Metadata
Metadata
Assignees
Labels
Fix AvailableA PR has been opened for this issueA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone